本页内容

CustomControl QML Type

定义自定义(非内置)控件的样式。更多...

Import Statement: import Qt.labs.StyleKit
Inherits:

ControlStyle

Status: Technology preview

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

属性

详细说明

CustomControl 允许您定义和设置样式,这些控件不属于 Qt Quick Controls。如果您有需要根据当前Style 和Theme 进行样式设置的额外控件或组件,这将非常方便。

与内置控件类型(如abstractButton 、pane 和slider )一样,CustomControl继承自ControlStyle 。与默认与控件类型隐式关联的内置类型不同,CustomControl需要显式设置controlType 。除此之外,它们的工作方式完全相同。

Style 或Theme 可以定义任意数量的自定义控件,而主题中的CustomControl可以拥有与Style 中相同的controlType 。这与,例如,slider 同时受到Style 和Theme 的样式设置并无二致。其回退逻辑也是相同的。

CustomControl 上未设置的任何样式属性都会回退到control 上设置的样式。

以下代码片段展示了如何为自定义控件定义样式:

// MyStyle.qml

Style {
    id: style
    readonly property int myControlType: 0
    CustomControl {
        controlType: style.myControlType
        background {
            width: 120
            height: 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.width
    implicitHeight: styleReader.background.height
    color: styleReader.background.color
    radius: styleReader.background.radius

    Text {
        font: styleReader.font
        anchors.centerIn: parent
        text: "ok"
    }
}

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

另请参阅 StyleReader 、ControlStyle 、Style 以及StylableControls::getControlStyle()。

属性文档

controlType : int

一个用于标识此自定义控件类型的唯一整数。请在自定义控件实现中的 `StyleReader.controlType ` 上设置相同的值,以将其与该样式定义关联。

自定义控件类型必须位于0 到100000 之间。

另请参阅 StyleReader.controlType 和StylableControls::getControlStyle()。

© 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.