Qt Quick Controls 配置文件

Qt Quick Controls support a special configuration file, qtquickcontrols2.conf , which is built into an application's resources.

The configuration file can specify the preferred style and certain style-specific attributes. The following example specifies that the preferred style is the 材质风格 . Furthermore, when the application is run with the Material style, its theme is light and the accent and primary colors are teal and blue grey, respectively. However, if the application is run with the Universal style instead, the accent color is red and the appropriate theme is chosen based on the system theme colors.

[Controls]
Style=Material
[Universal]
Theme=System
Accent=Red
[Material]
Theme=Light
Accent=Teal
Primary=BlueGrey
					

It is possible to specify a custom location for the configuration file with the QT_QUICK_CONTROLS_CONF 环境变量。

Controls Section

The following values can be specified in a Controls section of the configuration file:

变量 描述
Style Specifies the style to run the application with. The value can be the name of one of the 内置风格 自定义风格 .
FallbackStyle Specifies the style to use for controls that are not implemented. The style must be one of the 内置风格 . By default, the 基本 style is used.

Imagine Section

The following table lists values that can be used to configure the 想象风格 in an Imagine section of the configuration file:

变量 描述
路径 Specifies the path to the directory that contains the Imagine style assets. If not specified, the built-in assets are used.

For example, to specify a path to a directory stored in the 资源系统 :

[Imagine]
Path=:/imagine-assets
							

To specify a relative path to a local directory:

[Imagine]
Path=imagine-assets
							

注意: Due to a technical limitation, the path should not be named "imagine" if it is relative to the qtquickcontrols2.conf 文件。

Material Section

The following table lists values that can be used to configure the 材质风格 Material section of the configuration file:

变量 描述
Theme 指定默认 材质主题 . The value can be one of the available themes, for example "Dark" .
Variant

Specifies the Material variant. The Material Design has two variants: a normal variant designed for touch devices, and a dense variant for desktop. The dense variant uses smaller sizes for controls and their fonts.

The value can be "Normal" or "Dense" .

Accent 指定默认 Material accent color 。值可以是任何 color , but it is recommended to use one of the 预定义材质颜色 ,例如 "Teal" .
Primary 指定默认 Material primary color 。值可以是任何 color , but it is recommended to use one of the 预定义材质颜色 ,例如 "BlueGrey" .
Foreground 指定默认 材质前景颜色 。值可以是任何 color ,或某一 预定义材质颜色 ,例如 "Brown" .
Background 指定默认 材质背景颜色 。值可以是任何 color ,或某一 预定义材质颜色 ,例如 "Grey" .

Universal Section

The following table lists values that can be used to configure the Universal style Universal section of the configuration file:

变量 描述
Theme 指定默认 通用主题 . The value can be one of the available themes, for example "Dark" .
Accent 指定默认 Universal accent color 。值可以是任何 color , but it is recommended to use one of the 预定义通用颜色 ,例如 "Violet" .
Foreground 指定默认 通用前景颜色 。值可以是任何 color ,或某一 预定义通用颜色 ,例如 "Brown" .
Background 指定默认 通用背景颜色 。值可以是任何 color ,或某一 预定义通用颜色 ,例如 "Steel" .

字体配置

默认 font can be specified in a Font sub-group in each style's section in the configuration file. The Font sub-group can be defined in two alternative ways:

[Basic]
Font\Family=Open Sans
Font\PixelSize=20
[Material\Font]
Family=Open Sans
PixelSize=20
						

支持字体属性:

变量 描述
Family 字体系列 .
PointSize point size .
PixelSize pixel size .
StyleHint style hint 。可用值: SansSerif , Helvetica , Serif , Times , TypeWriter , Courier , OldEnglish , Decorative , Monospace , Fantasy , Cursive .
Weight weight . Qt uses a weighting scale from 1 to 1000 compatible with OpenType. A weight of 1 will be thin, whilst 1000 will be extremely black. Available pre-defined weights: Thin (100), ExtraLight (200), Light (300), Normal (400), Medium (500), DemiBold (600), Bold (700), ExtraBold (800), Black (900).
Style style 。可用值: StyleNormal , StyleItalic , StyleOblique .

Palette Configuration

默认 palette can be configured for each style using the Palette sub-group in the configuration file. The Palette sub-group can be defined in two alternative ways:

[Fusion]
Palette\Window=#dedede
Palette\WindowText=#212121
						

或:

[Fusion\Palette]
Window=#dedede
WindowText=#212121
						

Palette QML type for more information.

Using the Configuration File in a Project

In order to make it possible for Qt Quick Controls to find the configuration file, it must be built into application's resources using the Qt 资源系统 . Here's an example .qrc 文件:

<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
    <file>qtquickcontrols2.conf</file>
</qresource>
</RCC>
						

注意: Qt Quick Controls uses a file selector to load the configuration file. It is possible to provide a different configuration file for different platforms and locales. See QFileSelector 文档编制了解更多细节。

Finally, the .qrc file must be listed in the application's .pro file so that the build system knows about it. For example:

RESOURCES = application.qrc