简体   繁体   中英

How to edit only background-color for PyQT5 widgets

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_())

When the window have the focus:
在此处输入图像描述

When the window does not have the focus:
在此处输入图像描述

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.

use stylesheet for QProgressBar to achieve centered text and QProgressBar::chunk to keep background color even if it loses focus

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)
        # setting background color
        self.pbar.setStyleSheet(
                         "QProgressBar"
                          "{"
                          "text-align:center;"
                          "}"
                          "QProgressBar::chunk"
                          "{"
                          "background-color : blue;"
                          "text-align:center;"
                          "color : #E0E0E0;"
                          "border : 1px"
                          "}"
                         )
        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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM