Reputation: 1
I'm trying to change the background-color of my QPushButtons in PyQtDarkTheme, but it just doesn't work.
My real project I'm working on:
I've tried it like this, changing the text-color or everything else works fine, but not the background.
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton
import sys
import qdarktheme
qss = """
QPushButton {
background-color: red;
color: white;
}
"""
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
self.button = QPushButton("Press Me")
self.setCentralWidget(self.button)
app = QApplication(sys.argv)
qdarktheme.setup_theme(additional_qss=qss)
window = MainWindow()
window.show()
app.exec()
What am I doing wrong?
I know I could change every button individually with
button.setStyleSheet('QPushButton {background-color: red}')
but that causes a bunch of other problems I want to avoid.
Upvotes: 0
Views: 71
Reputation: 11
To get the background color to properly apply, replace "QPushButton" with "QPushButton:!window" in your custom QSS:
qss = """
QPushButton:!window {
background-color: red;
color: white;
}
"""
Explanation
In testing, it looks like the default stylesheet that pyqtdarktheme uses originally sets the background color of buttons using the :!window qualifier, alongside a number of other elements.
Qt will prioritize QSS selectors that are more 'specific', so to overwrite the default settings, we need to match the level of specification or more. Easiest way to do this is to match the original styling by replacing "QPushButton" with "QPushButton:!window"
Some more info on how QSS deals with conflicts here: https://doc.qt.io/qtforpython-6.7/overviews/stylesheet-syntax.html#conflict-resolution
Upvotes: 1