Unsolved Override value in stylesheet for specific QWidget
From here I learnt how to use css selectors to override the current style in my stylesheet. Now I have a different scenario when this method is quite hard to use.
At run-time I create a lot of
QWidgets(for this example let's say they are all
QLabel) and make a network connection to a remote machine. Whenever I receive data from the machine I parse it and I get a list of colors to be assigned to some of those hundreds of
QLabel. This happen every 100 ms.
I think it's quite hard to change the stylesheet for each one every time... is there a simpler way to override background and foreground (text) color for a
QLabelwith a stylesheet is already applied, without changing it?
The best would be use
QPalettebut unfortunately the stylesheet has the precedence.
Any other idea?
JonB last edited by
What do you mean by "with a stylesheet is already applied, without changing it"? You have to change the stylesheet if you want to change the color without going via
I do not know whether 100ms is very frequent for stylesheet changes, but you can always try....
I mean I load from a resource a stylesheet to change the appearance of the whole applications (colors, fonts, corners, etc...). But I would like to override a single property (i.e. background color of a specific widget) . I would go with
QPalette, but it doesn't work when a stylesheet is applied as far as I know.
From your answer I understand this is the only way to do it? It's a huge limitation for the Qt framework so much oriented to the user experience and appearance!
JonB last edited by JonB
I mean I load from a resource a stylesheet to change the appearance of the whole applications (colors, fonts, corners, etc...). But I would like to override a single property (i.e. background color of a specific widget)
I still don't get what you are trying to do here/have a problem with. I don't use "resources", but I presume that once you have set the stylesheet from resource initially that will have actually set the string returned by
QWidget::stylesheet()(http://doc.qt.io/qt-5/qwidget.html#styleSheet-prop)? So to change something you call
QWidget::stylesheet(), make your change, and then set that whole string back via
QWidget::setStylesheet()(I do this to alter individual styles). That means you should not have to re-fetch the resource. (Or maybe you mean your resource is only a stylesheet for the whole application, nothing for individual widgets? Do you understand that to achieve what you want we are talking about calling an individual widget's
setStylesheet(), not for the whole application?)
Don't know what you mean by "huge limitation". You have to do something to make the change, and since
QPaletteis overridden by an explicit stylesheet you need to do it the stylesheet way?
I could tell you how you could achieve this via "dynamic properties" on your widget (which I use for various reasons). However, since changing a dynamic property still requires you to call
QWidget::setStylesheet()to make the dynamic property update actually be reflected properly on the widget, I don't see the point....
Why not make a custom widget that draw some text using a color for bg and one for text color and simply
set it when data comes and call update() on it ?
It sounds like the usage of those labels are not part of the stylesheet concept for the user and
in reality, the label plays the role of some data-driven Status widget.
So i would just make a small custom Status widget and use that instead of labels since it
would be only a paintEvent override and a method to set 2 colors to be used.
Do you understand that to achieve what you want we are talking about calling an individual widget's setStylesheet(), not for the whole application?)
You got the point. I didn't understand that! I thought I had to mess with the quite complex stylesheet of the whole application. Now it makes more sense :-)
I was afraid that setting an individual stylesheet that defines only the background color would "reset" all the other application-level styles. This is why I didn't understand it before. But if it overrides just what I'm going to change it's perfect!
@mrjj Yes and no :-) Yes because the content of the widget is actually not related to the stylesheet (text and colors) but the style of the widget itself matters. For this reason I'm trying to use an already styled widgets (like
QLineEdit) in order to keep them like the other controls on the form.
Now that I understand what @JonB said (I apologize for the misunderstanding) I'm going to try the more obvious way using individual stylesheets.