本页内容

ControlStyle QML Type

定义控件在normal 状态下的样式。更多内容...

Import Statement: import Qt.labs.StyleKit
Inherits:

ControlStateStyle

Inherited By:

CustomControl

Status: Technology preview

此类型处于技术预览阶段,可能会发生变更。

属性

详细说明

ControlStyle 描述了Control 应采用的样式。其 API 与Qt Quick 控件的 API 基本一致:它提供了针对委托的分组属性,例如background 、indicator 、handle 和text ,以及布局属性,例如padding 和spacing 。如果您熟悉Qt Quick Controls 中Control 的 API,那么您应该会发现 ControlStyle 的 API 很容易理解。

ControlStyle 继承自ControlStateStyle ,因为它代表正常状态:直接设置在 ControlStyle 上的属性描述了当没有其他状态处于活动状态时控件的外观。针对特定状态的覆盖设置是通过嵌套状态实现的,例如hovered 、pressed 和checked 。

Each stylable control 在Style 、Theme 或StyleVariation 中,ControlStyle即为ControlStyle。例如,在下面的代码片段中,control 、button 和radioButton 都是ControlStyle:

Style {
    control {
        padding: 6
        text {
            color: "white"
        }
        background {
            radius: 4
            border.color: "gray"
        }
        indicator {
            width: 20
            height: 20
            border.width: 1
        }
        handle {
            width: 20
            height: 20
            radius: 10
        }
    }

    button {
        background {
            width: 120
            shadow.opacity: 0.6
            shadow.verticalOffset: 2
            shadow.horizontalOffset: 2
            shadow.color: "gray"
            color: "lightsteelblue"
            gradient: Gradient {
                GradientStop { position: 0.0; color: Qt.alpha("black", 0.0)}
                GradientStop { position: 1.0; color: Qt.alpha("black", 0.2)}
            }
        }
        hovered {
            background.color: "lightslategrey"
        }
        pressed {
            background.scale: 0.95
        }
    }

    radioButton {
    }

    checkBox {
    }

    slider {
    }

    // etc...
}

另请参阅 All stylable controls 、Style 、Theme 、StyleVariation 、ControlStateStyle 以及DelegateStyle 。

属性文档

variations : list<StyleVariation>

此控件类型的“type variations ”列表。

类型变体为作为该控件类型子控件(或后代控件)的控件提供了替代样式。例如,您可以使用它来为frame 中的所有buttons 设置与其他位置的按钮不同的样式:

Style {
    frame {
        variations: StyleVariation {
            button {
                text.color: "ghostwhite"
                background.border.width: 0
                background.color: "slategrey"
            }
        }
    }

    groupBox {
        // groupBox falls back to frame. Therefore, if the varations set on a
        // frame is not wanted on a groupBox, just override it and set it back to [].
        variations: []
    }
}

与实例变体不同——实例变体是通过“StyleVariation::variations ”附加属性从应用程序应用于特定控件实例的——类型变体则会应用于Style 中该控件类型的所有实例,无需应用程序进行额外配置。

另请参阅 StyleVariation 。

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