Qt select 如何设置默认样式?

How does Qt select a default style?

在 Qt GUI 应用程序中,QApplication::style()->objectName() 将 return 当前样式,例如 "windowsvista"。

How/where它是否选择这个默认样式,它使用什么信息来决定?

在文档中:

Qt contains a set of QStyle subclasses that emulate the styles of the different platforms supported by Qt (QWindowsStyle, QMacStyle etc.).

可以通过key设置样式:windowsvista例如fusionmacintosh等,使用任意key时,返回的样式都是Q风格。根据您使用的平台,您可以访问一定数量的密钥。

How/where does it choose this default style

这是在 QStyleFactory source file. You can also take a look a the QStyle source file 中完成的,以了解正在发生的事情。

what information does it use to decide

默认风格是平台相关的,然后你可以在这个平台上根据你的喜好选择任何风格。

Qt 带有内置样式,这些是(在我的 5.9.2 上):

  • Windows
  • Windows经验值
  • WindowsVista
  • Android
  • 融合
  • 苹果机

每个都有自己的 class,源自 QStyle

查看哪些可用(取决于 Qt 构建配置):

const auto & styles = QStyleFactory::keys();
for(const auto & s : styles)
{
    qDebug() << s;
}

如果存在自定义插件(即 QTDIR/plugins/styles 目录中的库),也会显示。

默认样式是怎么选择的?

QApplication方法style()中搜索默认样式,在qapplication.cpp文件中,顺序为:

  1. 样式覆盖,如果由环境变量 QT_STYLE_OVERRIDE 设置(在 QApplicationPrivate::process_cmdline() 中设置);
  2. QApplicationPrivate::desktopStyleKey() 返回的样式(此方法从当前平台主题加载样式列表和 select 此列表中出现在 QStyleFactory::keys() 列表中的名字);
  3. QStyleFactory::keys() 列表中的第一项。

如果无法确定样式,函数将断言

Q_ASSERT(!"No styles available!");