【发布时间】:2021-12-31 23:54:13
【问题描述】:
我想知道这里是否缺少一些东西。我在 Qt5 中使用属性来修改我的一些小部件样式,但它似乎在 Qt6 中不起作用。如果这是一个错误,我会在其他地方报告它,但我只是想知道我是否做错了什么。
我在下面有一个简单的例子,当 qt=5 时,属性更改成功触发样式更改,但在 qt=6 时不会。颜色应该随着数字的增加而改变。任何帮助表示赞赏!
Qt5 工作
Qt6 不工作
qt = 6
if qt == 6:
from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt6.QtCore import QTimer
elif qt == 5:
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt5.QtCore import QTimer
import sys
class SampleWidget(QWidget):
def __init__(self):
super().__init__()
self.timer1, self.timer2, self.timer3 = QTimer(), QTimer(), QTimer()
self.timer1.singleShot(1000, self.fun1)
self.timer2.singleShot(2000, self.fun2)
self.timer3.singleShot(3000, self.close) # noqa
self.label = QLabel('0')
font = QFont()
font.setPointSize(50)
self.label.setFont(font)
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.label)
self.setLayout(self.layout)
def fun1(self):
self.label.setText('1')
self.set_property(True)
def fun2(self):
self.label.setText('2')
self.set_property(False)
def set_property(self, style_red):
self.label.setProperty('StyleRed', style_red)
self.label.style().unpolish(self.label)
self.label.style().polish(self.label)
self.label.update()
QApplication.processEvents()
app = QApplication([])
app.setStyleSheet('QLabel[StyleRed=true]{color:red;} QLabel[StyleRed=false]{color:green;}')
gui = SampleWidget()
gui.show()
sys.exit(app.exec())
【问题讨论】: