Qt 的样式表性能命中

Pic*_*ico 5 qt stylesheet execution-time qtstylesheets

我想知道是否可以比在 Qt 应用程序中使用我的方法更快地加载样式表。

这是我的代码:

this->setStyleSheet("background-color : black;");
Run Code Online (Sandbox Code Playgroud)

执行这个简单的 css/qss 样式需要 270 毫秒。

使用此导入方法具有更大的 qss 样式

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

这个命令

this->setStyleSheet(css);
Run Code Online (Sandbox Code Playgroud)

花了 330 毫秒,所以考虑到执行的 css 样式块的差异,这还不错。

所以看起来 setStyleShet 命令的 init 很长。我的问题是:有没有办法加快这个命令的速度(不使用 Qstring、其他导入方法……)或线程?

对我来说这很重要,因为我需要经常更新我的样式表,而且它花费的时间与我执行的所有逻辑一样多。

谢谢。祝你今天过得愉快 :)

Pic*_*ico 3

找到了这个方法:

this->style()->unpolish(this); //"this" is my main window
this->style()->polish(this);
this->update();
Run Code Online (Sandbox Code Playgroud)

代替 :

this->setStyleSheet(css);
Run Code Online (Sandbox Code Playgroud)

速度快得令人难以置信!(0-1 毫秒 vs 150-200 毫秒)

解决方案就在那里:http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html#note-233