Qt如何应用外部Qt样式表文件中的样式? [英] How could Qt apply style from an external Qt Stylesheet file?

查看:247
本文介绍了Qt如何应用外部Qt样式表文件中的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户只需加载他们的 OWN Qt样式表文件即可自定义应用程序的默认外观.我们如何做到这一点?有人可以给我一个开端吗?

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?

推荐答案

说用户的样式表名为stylesheet.qss,位于应用程序文件夹中.

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

您可以在启动应用程序时使用-stylesheet参数加载样式表:

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.

然后您可以使用 QApplication :: setStyleSheet():

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

 qApp->setStyleSheet(StyleSheet);

Qt正在提供一个在线示例,这可能会有所帮助.

Qt is providing an example online which might be helpful.

这篇关于Qt如何应用外部Qt样式表文件中的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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