Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the use of styleSheets in a dynamic manner add a lot of computation

Tags:

qt

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?

like image 659
yan bellavance Avatar asked Dec 10 '09 19:12

yan bellavance


3 Answers

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.

like image 81
Ton van den Heuvel Avatar answered Oct 09 '22 18:10

Ton van den Heuvel


Yes. I found that with Qt 4.6.2 on Linux, setting a stylesheet to change the colour of the text on a QLabel is very slow.

The dynamic stylesheet looked like a great solution but for me, the necessary setStyle() was just as expensive as setStyleSheet()!

After much experimentation, I found this alternative to be at least twice as quick, and usually more than 50 times as quick:

QPalette palette = lbl->palette();
palette.setColor(QPalette::WindowText, Qt::gray);
lbl->setPalette(palette);

Depending on how your (static) stylesheet is set up, you'd have to replace QPalette::WindowText with QPalette::Window or QPalette::Button. See the QPalette documentation for details.

like image 2
Heath Raftery Avatar answered Oct 09 '22 19:10

Heath Raftery


In my experience stylesheets consume too much resources, better to avoid them if you can.

like image 1
Humberto Pinheiro Avatar answered Oct 09 '22 20:10

Humberto Pinheiro