I have a problem with styles in PyQT5. I would like to modify something in the "Fusion" style : when the page loses focus, the blue of some widgets becomes white, i would like to keep them blue.
But when i try to edit only the background color for a QprogressBar, the text is no more centered and there are some other changes.
(app.setStyleSheet("QProgressBar::chunk { background-color : blue}")
)
I also tried app.my_progress_bar.setStyleSheed("background-color : blue")
which seems to keep text centered but i don't know how to do it for "chunk" item.
Here is a little script if you want to test a solution :
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QWidget, QPushButton, QProgressBar, QVBoxLayout, QApplication
class Thread(QThread):
_signal = pyqtSignal(int)
def __init__(self):
super(Thread, self).__init__()
def __del__(self):
self.wait()
def run(self):
for i in range(100):
time.sleep(0.1)
self._signal.emit(i)
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.setWindowTitle('QProgressBar')
self.btn = QPushButton('Click me')
self.btn.clicked.connect(self.btnFunc)
self.pbar = QProgressBar(self)
self.pbar.setValue(0)
self.resize(300, 100)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.pbar)
self.vbox.addWidget(self.btn)
self.setLayout(self.vbox)
self.show()
def btnFunc(self):
self.thread = Thread()
self.thread._signal.connect(self.signal_accept)
self.thread.start()
self.btn.setEnabled(False)
def signal_accept(self, msg):
self.pbar.setValue(int(msg))
if self.pbar.value() == 99:
self.pbar.setValue(0)
self.btn.setEnabled(True)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyle("Fusion") ##### When the main windows loses focus, the progressbar becomes white instead of blue
ex = Example()
ex.show()
sys.exit(app.exec_())
There is no need to use style sheets as long as the color roles of the widget are known.
Specifically, QProgressBar normally uses the Highlight
role, which has a different color for the Inactive
color group, so you just need to override it.
palette = self.pbar.palette()
palette.setBrush(
palette.Inactive, palette.Highlight, palette.highlight())
self.pbar.setPalette(palette)
Note that the palette is only a reference, it's completely up to the style to decide which group/role use for a widget (or even completely ignore it). If you use another style than Fusion, the above might not work as expected.