[QUOTE=svenneve;28818]I’ve been using setProperty for these kind of styling things, acts like css class selectors.
.py
self.label = Label('Request')
self.label.setProperty('class', 'Request')
.qss
Label[class="Request"]
{
background-color: rgba(60,140,60,255);
border-bottom: 1px solid rgba(40,130,40,255);
border-top: 1px solid rgba(40,130,40,255);
}
[/QUOTE]
If you’re going to use properties in this fashion you may as well just use setObjectName(‘Request’) instead of a custom property. These will be accesible in the stylesheet via # symbol.
label#Request{
...
}
Properties are best used with changing states. For example…
# Initialize some process
my_widget.setProperty('processing', True)
my_widget.style().unpolish(my_widget)
my_widget.style().polish(my_widget)
my_widget.update()
# After process finishes
my_widget.setProperty('processing', True)
my_widget.style().unpolish(my_widget)
my_widget.style().polish(my_widget)
my_widget.update()
The unpolish > polish > update needs to be run each time you change the processing property to ensure it gets the appropriate attributes from your stylesheet. Here is what the stylesheet could look like.
[processing='true']{
color: red;
}
[processing='false']{
color: green;
}
It’d be best to remove the polishing boiler plate via a custom description. Here is a full example:
from PySide import QtGui
import sys
import signal
STYLE = '''
QLabel{
font: 48pt;
}
[processing='true']{
color: red;
}
[processing='false']{
color: green;
}
'''
class StyledProperty(object):
def __init__(self, name):
self.name = name
def __get__(self, inst, typ):
if inst:
return inst.property(self.name)
return
def __set__(self, inst, value):
inst.setProperty(self.name, value)
inst.style().unpolish(inst)
inst.style().polish(inst)
inst.update()
class MyLabel(QtGui.QLabel):
processing = StyledProperty('processing')
def __init__(self, *args, **kwargs):
super(MyLabel, self).__init__(*args, **kwargs)
self.processing = False
def mousePressEvent(self, event):
self.processing = not self.processing
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QtGui.QApplication(sys.argv)
app.setStyleSheet(STYLE)
label = MyLabel('Hello')
label.show()
sys.exit(app.exec_())