Qt Labs StyleKit
StyleKit 是Qt Quick 控件的声明式样式系统,构建于Qt Quick 模板之上。它可让您从单个Style 对象中为所有控件定义完整的可视化样式,包括支持themes 、state-based styling 和transitions 。StyleKit 可自动处理底层模板的实现,让您只需关注可视化方面,如colors 、dimensions 、borders 和shadows 。
StyleKit 的一个主要优势是它的分层属性系统:只需在基础类型(如abstractButton )上设置一次属性,它就会自动应用于所有按钮类控件。在需要时,可针对特定控件或状态覆盖该属性。对样式的更改会立即反映在所有控件上,从而确保一致性,同时还允许进行细粒度自定义。
对于需要超出StyleKit 提供的自定义行为的控件,您仍然可以实现自定义模板,并将其与StyleKit 风格控件无缝集成。
主要功能
- 声明式样式--易于使用的 QML API,让您专注于设计而非实现
- 分层回退--所有属性均可传播。只需设置一次,在需要时覆盖即可
- 基于状态的样式--为悬停、按下、聚焦等状态设计单独的外观。
- 动画过渡--定义状态间的平滑动画
- 主题支持--设计浅色和深色主题,以及任意数量的自定义主题
- 变体--设计相同控件的多种变体
- 调色板和字体集成--使用 QML 配置控件调色板和字体
下面的示例显示了样式的最小示例:
// PlainStyle.qml import QtQuick import Qt.labs.StyleKit Style { control { padding: 6 background { radius: 4 implicitWidth: 100 implicitHeight: 36 } indicator { implicitWidth: 20 implicitHeight: 20 border.width: 1 } handle { implicitWidth: 20 implicitHeight: 20 radius: 10 } } button { background { implicitWidth: 120 shadow.opacity: 0.6 shadow.verticalOffset: 2 shadow.horizontalOffset: 2 gradient: Gradient { GradientStop { position: 0.0; color: Qt.alpha("black", 0.0)} GradientStop { position: 1.0; color: Qt.alpha("black", 0.2)} } } pressed.background.scale: 0.95 } slider { indicator.implicitWidth: Style.Stretch indicator.implicitHeight: 6 indicator.radius: 3 } light: Theme { applicationWindow { background.color: "whitesmoke" } control { text.color: "black" background.color: "#e8e8e8" background.border.color: "#c0c0c0" hovered.background.color: "#d0d0d0" } button { text.color: "white" background.color: "cornflowerblue" background.shadow.color: "gray" hovered.background.color: "royalblue" } } dark: Theme { applicationWindow { background.color: Qt.darker("gray", 2.0) } control { text.color: "white" background.color: "#3a3a3a" background.border.color: "#555555" hovered.background.color: "#4a4a4a" } button { background.color: "sandybrown" background.shadow.color: "black" hovered.background.color: Qt.darker("sandybrown", 1.2) } } }
这就是如何在应用程序中设置样式:
// Main.qml import QtQuick import Qt.labs.StyleKit ApplicationWindow { id: app width: 1024 height: 800 visible: true // Assign the style to be used StyleKit.style: PlainStyle {} // Controls are used as normal Column { anchors.fill: parent anchors.margins: 10 spacing: 10 Button { text: "Button" } Slider { width: 200 } } }
在项目中使用模块
模块的 QML 类型可通过QtQuick.labs.StyleKit 导入。要使用这些类型,请在 .qml 文件中添加以下导入语句:
import Qt.labs.StyleKit
文章和指南
示例
- StyleKit 示例- 展示如何使用StyleKit 对Qt Quick 控件进行样式设置。
QML 类型
抽象基本类型,包含可样式化的控件类型 | |
抽象基本类型,具有样式和主题的共同属性 | |
定义委托的边框样式 | |
描述一个控件在给定状态下的样式 | |
定义正常状态下控件的样式 | |
定义控件的可样式属性 | |
定义自定义(非内置)控件的样式 | |
定义已命名的自定义主题 | |
定义控件中元素的视觉外观 | |
为开关、滑块和范围滑块等控件定义句柄样式 | |
定义委托的图像样式 | |
定义控件指示器的样式 | |
为委托定义阴影样式 | |
样式定义的根类型 | |
在状态转换期间动画样式属性变化 | |
用于设置和访问当前样式的单例 | |
跟踪控件样式属性的解析过程 | |
从特定控件的活动样式中读取属性 | |
为特定控件定义替代样式 | |
渲染委托样式 | |
定义子指示器委托的样式 | |
定义控件标签的文本样式 | |
为颜色方案定义颜色和样式覆盖 |
许可证
Qt LabsStyleKit 可在Qt Company 的商业许可下使用。此外,它还可以在GNU Lesser General Public License 第 3 版或GNU General Public License 第 2 版下使用。更多详情,请参阅Qt Licensing。
© 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.