CustomControl QML Type
定义自定义(非内置)控件的样式。更多
| Import Statement: | import Qt.labs.StyleKit |
| Inherits: |
属性
- controlType : int
详细说明
CustomControl 允许您定义和样式化不属于Qt Quick Controls 的控件。如果您有其他控件或组件,需要根据活动的Style 和Theme 进行样式设置,这将非常方便。
与内置控件类型(如abstractButton,pane, 和slider )一样,CustomControl 继承了ControlStyle 。与内建类型隐式连接到其控件类型不同,自定义控件需要明确设置controlType 。除此之外,它们的工作方式完全相同。
Style 或Theme 可根据需要定义任意多个自定义控件,而主题中的自定义控件可以与Style 中的自定义控件拥有相同的controlType 。这与slider 同时由Style 和Theme 定义样式没有区别。后退逻辑也是一样的。
任何未在 CustomControl 上设置的样式属性都会回退到在control 上设置的样式属性。
以下代码段展示了如何为自定义控件定义样式:
// MyStyle.qml Style { id: style readonly property int myControlType: 0 CustomControl { controlType: style.myControlType background { implicitWidth: 120 implicitHeight: 30 radius: 0 } hovered.background.color: "lightslategray" pressed.background.color: "skyblue" } }
下面的代码段展示了如何使用自定义控件实现样式的示例:
// Main.qml component MyControl : Rectangle { StyleReader { id: styleReader controlType: StyleKit.style.myControlType hovered: hoverHandler.hovered pressed: tapHandler.pressed palette: app.palette } HoverHandler { id: hoverHandler } TapHandler { id: tapHandler } implicitWidth: styleReader.background.implicitWidth implicitHeight: styleReader.background.implicitHeight color: styleReader.background.color radius: styleReader.background.radius Text { font: styleReader.font anchors.centerIn: parent text: "ok" } }
注意: Qt.labs 模块中的类型不保证在未来版本中保持兼容。
另请参阅 StyleReader,ControlStyle, 和Style 。
属性文档
controlType : int
标识此自定义控件类型的唯一整数。在自定义控件实现的StyleReader.controlType 上设置相同的值,以便将其连接到此样式定义。
自定义控件类型的范围必须是0 至100000 。
另请参阅 StyleReader.controlType 。
© 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.