Qt 获取不同 OS 的默认样式表

Qt get default style sheets for different OS

我正在开发一个跨平台应用程序,Windows 看起来比 Ubuntu 差多了。这只与 Windows 的默认样式 sheet 和 Ubuntu 的默认样式有关。有没有办法获得应用于应用程序的 OS 依赖样式 sheet?我试图通过以下方式检索应用程序样式 sheet:

QApplication app(argc, argv);
qDebug() << "Style sheet: " << app.styleSheet();

但这会产生一个空字符串(除非您手动将其设置为某些内容)。我的目的是检索 Ubuntu 样式 sheet 并将其保存到文件中(并将其添加到资源列表中),这样我就可以在 OS 中为应用程序加载它:

QApplication app(argc, argv);
QFile file(":/ubuntu_style.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
app.setStyleSheet(styleSheet);

关于如何获得 OS 的默认样式 sheet 的任何想法?

根据 documentation,当您未使用参数 -stylesheet 指定有效路径时,stylesheet() 将 return 一个空字符串(这是您的情况) .我认为你不这样做。因此,您需要为您的应用程序添加此参数。或者,制作您自己的 qss 文件并将其添加到资源文件夹中。 希望能帮到您解决问题。

我在 another forum 中找到了答案。我post把它放在这里是为了以后搜索其他用户。

您可以使用 QStyleFactory. A list of possible style is available using QStyleFactory::key() returns 可用键列表获得 OS 默认样式。在我的例子中,这总是在 Windows 和 Linux OS 中返回 "Windows""Fusion"。而且,在Windows里,我还有"windowsvista"。使用此 class,我可以 设置应用程序主样式:

QApplication app(argc, argv);
app.setStyle(QStyleFactory::create("Fusion"));

所以我的应用程序看起来总是一样的,不管它在 OS 中 compiled/used。

然而,这并没有提供样式sheet,它提供了QStyle, so using app.styleSheet(); returns an empty string. Reading through another Stack Overflow , and especially the comments to its it is possible to better understand QStyle and qt style sheets. And conclude that, it is not possible to convert a QStyle into a style sheet as these are things working on different layers. (special thanks to the comment posted in there by @lcikgl)