C++ Qt 如何应用来自外部 Qt 样式表文件的样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4448236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could Qt apply style from an external Qt Stylesheet file?
提问by Owen
I would like the users to be able to customize the default look of our applications by simply loading their OWNQt Style-sheet files. How do we accomplish that? Can anybody give me a head start?
我希望用户能够通过简单地加载他们自己的Qt 样式表文件来自定义我们应用程序的默认外观。我们如何做到这一点?有人可以给我一个开端吗?
回答by Jér?me
Say the user have its stylesheet named stylesheet.qss
and is located in the application folder.
假设用户将其样式表命名stylesheet.qss
并位于应用程序文件夹中。
You could load the style sheet when starting the application, using the -stylesheet argument :
您可以在启动应用程序时使用 -stylesheet 参数加载样式表:
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():
然后,您可以打开此文件,加载内容,并使用QApplication::setStyleSheet()将其设置为您的应用程序:
QFile File("stylesheet.qss");
File.open(QFile::ReadOnly);
QString StyleSheet = QLatin1String(File.readAll());
qApp->setStyleSheet(StyleSheet);
Qt is providing an example onlinewhich might be helpful.
Qt 提供了一个在线示例,可能会有所帮助。
回答by James Gaunt
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
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.
您可以从任意数量的位置设置/获取此配置,应用程序中的属性对话框可能是最自然的方法。