How could Qt apply style from an external Qt Stylesheet file?

寵の児 提交于 2020-05-07 10:32:19

问题


I would like the users to be able to customize the default look of our applications by simply loading their OWN Qt Style-sheet files. How do we accomplish that? Can anybody give me a head start?


回答1:


Say the user have its stylesheet named stylesheet.qss and is located in the application folder.

You could load the style sheet when starting the application, using the -stylesheet argument :

myapp->stylesheet = stylesheet.qss;

But this require your user to know how to start an application with arguments.

What you could also do is to add a settings dialog in your app, where the user can choose a stylesheet path.

You can then open this file, load the content, and set it to your application with QApplication::setStyleSheet() :

 QFile File("stylesheet.qss");
 File.open(QFile::ReadOnly);
 QString StyleSheet = QLatin1String(File.readAll());

 qApp->setStyleSheet(StyleSheet);

Qt is providing an example online which might be helpful.




回答2:


You just set the style sheet for the entire application based on configuration provided by the customer.

http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop

You could set/get this configuration from any number of places, a properties dialog in the application is probably the most natural approach.



来源:https://stackoverflow.com/questions/4448236/how-could-qt-apply-style-from-an-external-qt-stylesheet-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!