使用 pyqt 更新 QThread 中 QTextEdit 的样式 Sheet

Update the Style Sheet of a QTextEdit in QThread with pyqt

我的 GUI 由各种 Qlabels 和 Qtext 组成,它们由它自己的 QThread 更新,QThread 本质上是状态机。我的规范要求随着状态的变化,每个线程都需要用新文本独立地更新它的 QLabels / QText 组,并将它的背景颜色更改为红色、黄色或绿色。我在颜色更改方面遇到问题:

代码:

red_alert = "QText {Edit font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;bgcolor=red}"

class TesterThread(QtCore.QThread):
    updateText = QtCore.pyqtSignal(str)
    updateColor = QtCore.pyqtSignal(str)

    def __init__(self, thread_number, parent=None):
        super(TesterThread, self).__init__(parent)
        self.color = "red"
        self.status = "Disconnected"
        self.t_number = thread_number
        self.connection = False
        self.testing = False
        self.complete = False

    def run(self):
        self.tester()
    def tester(self, restart=False):
        if restart:
            logging.debug("Thread {}:Restarting for testing".format(self.t_number))
        else:
            logging.info("Thread {}:Ready for testing".format(self.t_number))
        # Init state, no device connected
        while not self.connection:
            self.updateText.emit("Status : {}".format(self.status))
            self.updateColor.emit("{}".format(thread_gui.red_alert))
            self.connection = True
            self.status = "Connected"
            self.updateText.emit("Status : {}".format(self.status))
            self.testing = True
        # Device connected, starting test
        while self.testing:
            self.status = "Testing"
            self.updateText.emit("Status : {}".format(self.status))
            self.testing = False
            self.complete = True
        # Test complete, waiting for unit removal
        while self.complete and self.connection:
            self.status = "Reset"
            self.updateText.emit("Status : {}".format(self.status))
            time.sleep(5)
            self.complete = False
            self.connection = False
            self.status = "Disconnected"
        # Unit remove, restart loop for next test.
        self.tester(restart=True)

界面:

class TestSuiteGUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        self._threads = []
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.com_ports_list = serial_lib.get_com_ports()
        self.ui.setupUi(self)
        self.update_comm_fields(self.com_ports_list)
        logging.info("Spinning up threads...")
        for num, com_port_chunk in zip(range(1, 25), self.com_ports_list):
            tester_thread = TesterThread(thread_number=num)
            status_box = getattr(self.ui, 'status_{}'.format(num))
            tester_thread.updateText.connect(status_box.setText)
            status_box = getattr(self.ui, 'status_{}'.format(num))

            tester_thread.updateColor.connect(status_box.setStyleSheet)

            tester_thread.start()
            self._threads.append(tester_thread)

您的样式表看起来无效。

QText {
    Edit font-family:'MS Shell Dlg 2'; 
    font-size:8.25pt; 
    font-weight:400; 
    font-style:normal;
    bgcolor=red
}

我猜你想要

QTextEdit {
    font-family: "MS Shell Dlg 2"; 
    font-size: 8.25pt; 
    font-weight: 400; 
    font-style: normal;
    background-color: red;
}

更改样式表时,最好先在 Qt Designer 中试用它们,以确保它们看起来像您期望的那样。