本页内容

CustomTheme QML Type

定义一个命名的自定义主题。更多...

Import Statement: import Qt.labs.StyleKit
Status: Technology preview

此类型处于技术预览阶段,可能会有变动。

属性

详细说明

CustomTheme 允许您在light 和dark 之外添加额外的主题。虽然浅色和深色主题会根据操作系统的配色方案自动应用automatically ,但 CustomTheme 必须由应用程序显式激活。除这一区别外,所有主题的工作原理均相同。

您可以在Style 中定义任意数量的自定义主题。

Style {
    CustomTheme {
        name: "HighContrast"
        theme: Theme {
            control.background.color: "white"
            control.background.border.color: "black"
            control.background.border.width: 2
        }
    }

    CustomTheme {
        name: "Sepia"
        theme: Theme {
            control.text.color: "#5b4636"
            control.background.color: "#f4ecd8"
            control.background.border.color: "#c8b99a"
            applicationWindow.background.color: "#efe6d0"
        }
    }
}

要激活 CustomTheme,请在应用程序中将Style::themeName 设置为其名称:

ComboBox {
    model: StyleKit.style.availableThemeNames
    onCurrentTextChanged: StyleKit.style.themeName = currentText
}

您还可以在启动时将 CustomTheme 设置为默认主题:

ApplicationWindow {
    width: 1024
    height: 800
    visible: true

    StyleKit.style: MyStyleKitStyle {
        themeName: "HighContrast"
    }
}

在 Style 中定义的自定义主题可在运行时通过 `Style::customThemeNames ` 或 `Style::availableThemeNames` 进行查询。

注意: Qt.labs 模块中的类型在未来版本中不保证保持兼容性。

另请参阅 Theme 、Style::themeName 以及Style::customThemeNames 。

属性文档

name : string

该主题的名称。这是您在Style::themeName 中设置的值,用于激活该主题。

theme : Component

定义主题的Theme 组件。该组件仅在主题被激活时才会被实例化。

主题中未设置的属性将回退到 `Style` 中定义的属性。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.