I define style sheet and test.py like below.
I expected the label's text padding change 10px -> 100px but its not applied. The background color changed to RED. I test color, font-size as well, but only 'padding' didn't applied after click the button.
Can anyone explain why only padding not applied in style sheet?
style
.QLabel[text="OK"]{
background-color: rgb(0,255,0);
color: rgb(255,250,250);
font-size: 24pt;
padding: 10px;
}
.QLabel[text="ERR"]{
background-color: rgb(255,0,0);
color: rgb(255,250,250);
font-size: 24pt;
padding: 100px;
}
test.py
from PySide6 import QtWidgets
from PySide6.QtCore import Qt
class MyWindow(QtWidgets.QWidget):
def set_style(self):
with open('./style', 'r') as f:
self.setStyleSheet(f.read())
def __init__(self):
super().__init__()
self.set_style()
self.setupUi()
def setupUi(self):
self.label = QtWidgets.QLabel('OK', self)
self.label.setAlignment(Qt.AlignCenter)
self.button = QtWidgets.QPushButton('Button', self)
vlayout = QtWidgets.QVBoxLayout(self)
vlayout.setAlignment(Qt.AlignCenter)
vlayout.addWidget(self.label)
vlayout.addWidget(self.button)
self.button.clicked.connect(self.btn_clicked)
self.show()
def btn_clicked(self, a):
self.label.setText("ERR")
self.label.style().unpolish(self.label)
self.label.style().polish(self.label)
self.update()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
win = MyWindow()
app.exec_()

