Here it's suggested to hard-code the link color of the application's palette. But it isn't very elegant to have to define a color that is outside of the style sheet file.
Luckily Qt's style sheets offer a way to respect a property set to a widget. So, the qss file can contain this:
Code: Select all
QLabel[haslink="true"] {
color: green;
}
When creating a QLabel in C++ or Python code that sets the user-defined property "haslink" to "true" and the style sheet is applied then the text is shown in green.
Code: Select all
from PySide import QtGui
m = QtGui.QLabel()
m.setProperty("haslink", "true")
m.setText("Hello")
m.setStyleSheet("QLabel[haslink='true']{color: green}")
m.show()
But the second problem is that the item of the property editor is not a widget. Thus, there is no way to directly set a color depending on a property.
So, a (bit hackish) workaround is to implement a sub-class of QLabel and re-implement its paintEvent() function. The default implementation creates an instance of QStyleOptionFrame locally which will contain the currently used text color. Now the trick to be able to access the color is that we make QStyleOptionFrame a member of the sub-class and as soon as the widget gets a paint event we get the current text color.
Code: Select all
class MyLabel(QtGui.QLabel):
def __init__(self):
super(MyLabel, self).__init__()
self.opt = QtGui.QStyleOptionFrame()
def paintEvent(self, ev):
p = QtGui.QPainter(self)
self.initStyleOption(self.opt)
self.style().drawControl(QtGui.QStyle.CE_ShapedFrame, self.opt, p, self)
m=MyLabel()
m.setProperty("haslink", "true")
m.setText("Hello")
m.setStyleSheet("QLabel[haslink='true']{color: green}")
m.opt.palette.color(QtGui.QPalette.Text)
m.show()
m.opt.palette.color(QtGui.QPalette.Text)
Once we have this color we set the link color of the application's palette.