Mercurial > hg > tilerswift
changeset 54:2e9f6582c3d2
ColourButton: use complete CSS instead overriding platfrom style
This is as suggested by this answer:
https://stackoverflow.com/a/57846090/11352427
Seems like it's still possible to override system styles as long as we
set all parts of the CSS. The key seems to be to also set the radius
so that Qt will recolour the button.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Sun, 08 Sep 2019 18:43:19 -0400 |
parents | 4a4c74d920bc |
children | 47680c83e0e0 |
files | tilerswift |
diffstat | 1 files changed, 14 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/tilerswift +++ b/tilerswift @@ -4,7 +4,6 @@ from colors import NES_PALETTE -PLASTIQUE = QtWidgets.QStyleFactory().create("Plastique") def read_rom(filename): with open(filename, 'rb') as f: @@ -279,20 +278,28 @@ self.colour_idx = colour_idx self.set_colour(colour_idx) - # Native styles sometimes don't allow recolouring the - # background, see https://bugreports.qt.io/browse/QTBUG-11089 - self.setStyle(PLASTIQUE) - def set_colour(self, colour_idx): self.colour_idx = colour_idx self.setText(f"{colour_idx:0{2}X}") bgcolour = NES_PALETTE[colour_idx] qt_colour = QtGui.QColor(bgcolour) - [r, g, b] = qt_colour.red(), qt_colour.green(), qt_colour.blue() + r, g, b = qt_colour.red(), qt_colour.green(), qt_colour.blue() luminance = (0.2126*r + 0.7152*g + 0.0722*b)/256 textcolour = 'white' if luminance < 0.5 else 'black' - self.setStyleSheet(f"color: {textcolour}; background-color: {bgcolour}") + bordercolour = "#444444" if r == g == b == 0 else qt_colour.darker().name() + self.setStyleSheet( + f''' + QPushButton {{ + color: {textcolour}; + background: {bgcolour}; + border: 2px outset {bordercolour}; + border-radius: 4px; + }} + QPushButton:pressed {{ + border-style: inset; + }}''' + ) class PaletteButton(ColourButton):