tags:

views:

100

answers:

1

I have about 40 buttons that are also indicators (On or Off) and once a second I refresh the color of these indicators depending on the state. I do this by changing the stylesheet. Is it over kill to do this and if so should I only set a new stylesheet when it the indicator has changed state or should I use something like QBrush?

+4  A: 

Do not dynamically set complete stylesheets. Instead, define an application wide stylesheet using dynamic stylesheet that you parse once at application startup. Then, in the stylesheet, define dynamic stylesheet properties as detailed in the documentation:

There are many situations where we need to present a form that has mandatory fields. To indicate to the user that the field is mandatory, one effective (albeit esthetically dubious) solution is to use yellow as the background color for those fields. It turns out this is very easy to implement using Qt Style Sheets. First, we would use the following application-wide style sheet:

*[mandatoryField="true"] { background-color: yellow }

In your case, you could probably do something like this:

QPushButton[state="on"] {
  background-color: green;
}

QPushButton[state="off"] {
  background-color: red;
}

Then update the button 'state' property:

pushButton->setProperty("state", "on");
pushButton->setStyle(QApplication::style());

Unfortunately, for Qt 4.6 you will need to force a recomputation of the stylesheet by resetting the style of the widget, hence the setStyle() call.

Using dynamic stylesheets in this way is very fast. I am working on an application that makes heavy use of dynamic stylesheet properties and have not noticed any performance degredation.

Ton van den Heuvel
Thanks for the great answer. So from Qt 4.6 I will have to call pushButton->setStyle(QApplication::style()); everytime I change the properties to affect the style? I will put it in my code right now to avoid future problems.
yan bellavance
are you absolutely sure that this will be the case in Qt 4.6?
yan bellavance
I have not managed to get the dynamic behaviour to work without resetting the style. Also see this question: http://stackoverflow.com/questions/1595476/are-qts-stylesheets-really-handling-dynamic-properties/1849127#1849127Did you manage to get the dynamic behaviour without resetting the widget style? I am using Qt under Linux, not sure whether that makes any difference.
Ton van den Heuvel