I would like to know if it's possible to load stylesheets a lot faster than using my method in a Qt app.

Here my codes:

this->setStyleSheet("background-color : black;");

Took 270 ms to execute this simple css/qss styling.

With a lot bigger qss styling using this import method

QFile file("style.qss");  
if(!file.open(QFile::ReadOnly)){  
    qDebug() << "Style QSS file not found";  
}  
css = QString::fromLatin1(file.readAll());  
file.close(); 

this command

this->setStyleSheet(css);

took 330ms so it's not that bad considering the difference of css styling blocks executed.

So it looks like the init of the setStyleShet command is very long. My question is: Is there a way of speeding up this command (by not using Qstring, other import methods,....) or by threading ?

For me it's huge because I need to update often my stylesheets and it's taking as much time as all my logic executed.

Thanks. Have a nice day :)

有帮助吗?

解决方案

Found this method :

this->style()->unpolish(this); //"this" is my main window
this->style()->polish(this);
this->update();

instead of :

this->setStyleSheet(css);

It's incredibly fast ! (0-1 ms vs 150-200ms)

Solution was there: http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html#note-233

其他提示

In Linux there can be some serious performance hits using stylesheets. Here's some information:

http://kjellkod.wordpress.com/2011/05/22/moving-to-qt5-will-that-remove-qts-performance-issues-on-linux/

You can try switching your graphics rendering engine to raster. You can do this a few different ways. The easiest way to test it is to set it via command line argument when running your app:

yourApp -graphicssystem raster

To do it in code here's the API:

http://doc.qt.digia.com/4.5-snapshot/qapplication.html#setGraphicsSystem

Although I use CSS in QT a lot I still have concerns about if extensive CSS is a right thing to do in Qt..

I would strongly suggest try to limit CSS usage or at least think very carefully then designing them..

Especially for complex application with a lot of controls and complicated layouts you should be very very careful in using them if you target cross-platforms environments. It's indeed very fast way to go if you need to 'paint' nicely an application targeting only Win32 or MacOSX for example, but running same CSS on other platform could result in even more uglier UI then using native controls.

Then you start working on interfaces where 'each pixel matters' you will see that even basic layouts in Qt has mistakes and adding on top CSS engine which also full of 'small' bugs could lead to even more unpredictable results and development time.

Regarding your question - setting a global CSS on an application level is a fastest thing you can get, BUT remember that it will be inherited within all application widgets so in case you need something different you will have to create special rules.

I tried the polish & unpolish method, but not worked. In the end, I tried qApp->setStyleSheet("style.."); if it’s a small project it worked.
you can reference another similar answer, using dynamic stylesheet as Ton van den Heuvel answered, and replace pushButton->setStyle(QApplication::style()); by widget ->setStyle(widget->style())
wish it will help :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top