Qt样式表管理和性能 [英] Qt Stylesheet management and performances

查看:369
本文介绍了Qt样式表管理和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Qt样式表的管理有一个问题。



我有一个大Qt应用程序可以动态改变它的颜色主题。为了做到这一点,我创建了几个样式表(一个主题 - 他们是相当大)。当用户想改变颜色主题时,他点击一个调用 QWidget 方法 setStylesheet(QString) MainWindow 。这实际上是工作,但我的GUI冻结8-10秒的过程。



为了减少延迟,我试图使用 unpolish(QApplication * application) QStyle :: polish(QApplication * application)。性能是相当令人印象深刻(不到一秒),但几个 Widget 属性不更新,例如。 QToolButton 的图标属性。此外,即使所有我的自定义小部件继承自常见的小部件类( QFrame Qwidget QStackedWidget etc ...)。
我用 polish 方法错过了什么吗?

解决方案

您可以使用 setPalette QPalette

  void QApplication :: setPalette const QPalette& palette,const char * className = 0)[static] 




将默认应用程序调色板更改为调色板。



调色板可以根据
QStyle :: polish p>

警告:不要将此功能与Qt样式表配合使用。
当使用样式表时,窗口部件的调色板可以使用color,background-color,selection-color,
selection-background-color和alternate-background-color。



注意:一些样式不使用所有绘图的调色板,对于
实例,如果他们使用native主题引擎。


如果要从一个地方更改所有窗口小部件的主题,这是一个好方法。

我在大的应用中使用它,它的工作不错。



但是如果你将styleSheet或者palette设置为一个小部件,它不会得到你的一般主题。

  QPalette myPalette; 
myPalette.setColor(QPalette :: Background,Qt :: red);
myPalette.setColor(QPalette :: WindowText,QColor(150,150,150));
qApp-> setPalette(myPalette);


I have an issue with the management of Qt style sheets.

I have a big Qt application that can change its colors theme dynamically. To do that, I created several style sheets (one by theme - they are quite big). When a user wants to change the color theme, he clicks on a button that calls the QWidget method setStylesheet(QString) of the MainWindow. This actually works but my GUI freezes 8-10 seconds in the process.

To reduce the delay, I've tried to use unpolish(QApplication * application) and QStyle::polish(QApplication * application). The performance is quite impressive (less than one second) but several Widget properties are not updated, e.g. the icon property of the QToolButton. Moreover all my custom widgets are not updated even if they inherits from common widget classes (QFrame, Qwidget, QStackedWidget etc...). Do I miss something with the polish method? Is there another way to perform a better update of the style of my application?

解决方案

You can use setPalette( QPalette )

void QApplication::setPalette ( const QPalette & palette, const char * className = 0 ) [static]

Changes the default application palette to palette.

The palette may be changed according to the current GUI style in QStyle::polish().

Warning: Do not use this function in conjunction with Qt Style Sheets. When using style sheets, the palette of a widget can be customized using the "color", "background-color", "selection-color", "selection-background-color" and "alternate-background-color".

Note: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines.

If you want to change theme of all widgets from one place, this is good way.
I use this in big application and it's working good.

But if you set styleSheet or palette to a widget, it is not getting your general theme.

QPalette myPalette;
myPalette.setColor(QPalette::Background, Qt::red);
myPalette.setColor(QPalette::WindowText, QColor(150, 150, 150));
qApp->setPalette(myPalette);

这篇关于Qt样式表管理和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆