DelegateStyle QML Type
定义控件中某个元素的视觉外观。更多...
| Import Statement: | import Qt.labs.StyleKit |
| Inherited By: | |
| Status: | Technology preview |
此类型处于技术预览阶段,可能会有变动。
属性
- alignment : Qt::Alignment
- border : BorderStyle
- bottomLeftRadius : real
- bottomMargin : real
- bottomRightRadius : real
- clip : bool
- color : color
- data : QObject
- delegate : Component
- fillHeight : bool
- fillWidth : bool
- gradient : Gradient
- height : real
- image : ImageStyle
- leftMargin : real
- margins : real
- minimumHeight : real
- minimumWidth : real
- opacity : real
- radius : real
- rightMargin : real
- rotation : real
- scale : real
- shadow : ShadowStyle
- topLeftRadius : real
- topMargin : real
- topRightRadius : real
- visible : bool
- width : real
详细说明
DelegateStyle 描述了ControlStyle 中的视觉元素的外观,例如其background 、indicator ,或indicator.foreground 等子元素。它提供了用于控制size 、color 、border 、radius 、shadow 、image 、opacity 等属性的设置。
注意: Qt.labs 模块中的类型在未来版本中不保证保持兼容性。
另请参阅 ControlStyle 、ControlStateStyle 以及《FallbackStyle 参考》。
属性文档
alignment : Qt::Alignment
委托在其父对象中的对齐方式。默认值为Qt.AlignLeft | Qt.AlignVCenter 。
border : BorderStyle
用于设置此委托边框样式的分组属性。
bottomLeftRadius : real
左下角的圆角半径。如果未设置,则默认使用radius 。
另请参阅 radius 、topLeftRadius 、topRightRadius 以及bottomRightRadius 。
bottomMargin : real
该委托对象的底部边距。如果未设置,则默认采用margins 。
另请参阅 margins 、topMargin 、leftMargin 以及rightMargin 。
bottomRightRadius : real
右下角的圆角半径。如果未设置,则默认采用radius 。
另请参阅 radius 、topLeftRadius 、topRightRadius 以及bottomLeftRadius 。
clip : bool
代理是否截断其内容。默认值为false 。
color : color
此委托的填充颜色。默认值为transparent 。
与“快速渐变”(QuickRectangle )不同,后者会用渐变替换原有颜色,而 StyleKit 则会在颜色之上绘制渐变(gradient )。这意味着您可以使用半透明的渐变作为叠加层(例如,产生微妙的阴影效果),同时底下的颜色依然可见。
button {
background.gradient: Gradient {
GradientStop { position: 0.0; color: Qt.alpha("black", 0.0)}
GradientStop { position: 1.0; color: Qt.alpha("black", 0.2)}
}
background.color: "lightsteelblue"
hovered.background.color: Qt.darker("lightsteelblue", 1.1)
pressed.background.color: Qt.darker("lightsteelblue", 1.2)
}data : QObject
一个任意对象,可用于将自定义数据从样式传递给自定义的delegate 组件。
这使您能够定义超出 StyleKit API 所提供范围的自定义样式属性。该数据对象可在不同状态和主题之间变化,从而能够为自定义delegate 中的元素设置样式,而这些样式无法通过内置属性实现。
以下代码片段使用了一个自定义委托,该委托绘制了一个叠加的Text ,其颜色会根据控件的状态而变化。该委托继承了StyledItem ,虽然该继承是可选的,但可确保按钮背景的其余部分正常渲染。
component OverlayData : QtObject {
property color overlayColor
}
toolButton {
background.delegate: StyledItem {
id: custom
Text {
color: custom.delegateStyle.data.overlayColor
font.pixelSize: 30
text: "シ"
}
}
background.data: OverlayData {
overlayColor: "sandybrown"
}
hovered.background.data: OverlayData {
overlayColor: "magenta"
}
}注意: data 对象 会作为整体进行传播。与常规样式属性不同,数据对象内的各个属性不会单独传播。
另请参阅 delegate 。
delegate : Component
用于在Qt Quick 控件中渲染DelegateStyle 的委托。
默认值为null ,此时将改用StyledItem 进行渲染。若要完全隐藏该委托(并阻止其被创建),请将visible 设置为false 。
该委托需要定义以下必填属性:
required property DelegateStyle delegateStyle
required property QtObject controldelegateStyle 指向描述委托样式设置的DelegateStyle 。control 指向拥有该委托的Qt Quick 控件。后者可用于获取样式中未提供的有关该控件的额外信息。
如果您知道拥有控件的具体类型,可以使用该类型代替 `QtObject ` 作为 `control ` 属性。例如,由于以下代码片段中的 handle 委托总是在 Slider 内部使用,因此该类型可以设置为 `T.Slider`:
// import QtQuick.Templates as T
slider {
handle.delegate: Rectangle {
id: handle
required property DelegateStyle delegateStyle
required property T.Slider control
implicitWidth: delegateStyle.width
implicitHeight: delegateStyle.height
radius: delegateStyle.radius
color: delegateStyle.color
Text {
anchors.centerIn: parent
text: handle.control.value.toFixed(0)
}
}
}注意:如果 DelegateStyle 定义了drop shadow ,它将由shadow delegate.
另请参阅 data 、visible 和StyledItem 。
fillHeight : bool
委托控件是否应填满父项的可用高度。
某些委托控件(例如复选框指示器的“前景”部分)通常需要填充指示器的可用高度(和宽度),而非采用固定高度。这可以通过将fillHeight 设置为true 来实现。若要在委托控件与其父控件边缘之间添加间距,请使用margins 属性。
如果 `fillHeight ` 设置为 `false`,则委托的高度由其 `height` 决定。
fillWidth : bool
委托方法是否应填满父项的可用宽度。
某些委托(例如Slider 的凹槽)通常需要填满控件的可用宽度,而不是具有固定的隐式宽度。这可以通过将fillWidth 设置为true 来实现。若要在委托与其父项边缘之间留出一些空间,请使用margins 属性。
如果fillWidth 的值为false ,则委托的宽度由其width 决定。
另请参阅 fillHeight 和width 。
gradient : Gradient
此委托的渐变效果。默认值为null (无渐变)。
与“快速渐变”(QuickRectangle )不同,后者是用渐变替换颜色,而StyleKit是在颜色之上绘制渐变。这意味着您可以使用半透明的渐变作为叠加层(例如,微妙的阴影效果),同时底下的颜色依然可见。
button {
background.gradient: Gradient {
GradientStop { position: 0.0; color: Qt.alpha("black", 0.0)}
GradientStop { position: 1.0; color: Qt.alpha("black", 0.2)}
}
background.color: "lightsteelblue"
hovered.background.color: Qt.darker("lightsteelblue", 1.1)
pressed.background.color: Qt.darker("lightsteelblue", 1.2)
}另请参阅 color 。
height : real
该委托的高度。
注意: 仅当fillHeight 的false 为true时,此 属性才会设置委托的高度。当fillHeight 在样式层次结构中更靠上的位置被设置时(例如在fallback style 中),此规则同样适用。
如果该委托位于控件的根节点——例如background 或indicator ——则在计算控件的implicit height 时,它也会被包含在内,即使fillHeight 的值是true 。
另请参阅 width 、fillHeight 、minimumHeight 以及StyleKit 属性解析。
image : ImageStyle
用于将图像放置在此委托器内的分组属性。
另请参阅 ImageStyle 。
leftMargin : real
该委托对象的左边距。如果未设置,则默认使用margins 。
另请参阅 margins 、rightMargin 、topMargin 以及bottomMargin 。
margins : real
该委托周围的统一边距。设置此选项将为leftMargin 、rightMargin 、topMargin 和bottomMargin 提供默认值。每侧的边距均可单独覆盖。
另请参阅 leftMargin 、rightMargin 、topMargin 和bottomMargin 。
minimumHeight : real
此委托对象的最小高度。委托对象的大小不会小于该值。
minimumWidth : real
此委托的最小宽度。委托的大小不会小于此值。
opacity : real
此委托的透明度,范围从0.0 (完全透明)到1.0 (完全不透明)。默认值为1.0 。
radius : real
应用于该委托所有四个角的圆角半径。可以通过topLeftRadius 、topRightRadius 、bottomLeftRadius 和bottomRightRadius 重写各个角的圆角半径。
另请参阅 topLeftRadius 、topRightRadius 、bottomLeftRadius 和bottomRightRadius 。
rightMargin : real
该委托对象的右边距。如果未设置,则回退到margins 。
另请参阅 margins 、leftMargin 、topMargin 以及bottomMargin 。
rotation : real
该代理的旋转角度(单位:度)。
scale : real
此委托人的缩放因子。默认值为1.0 。
shadow : ShadowStyle
用于为该委托设置背面阴影样式的分组属性。
另请参阅 ShadowStyle 。
topLeftRadius : real
左上角的圆角半径。如果未设置,则默认采用radius 。
另请参阅 radius 、topRightRadius 、bottomLeftRadius 以及bottomRightRadius 。
topMargin : real
此委托对象的顶部边距。若未设置,则默认采用margins 。
另请参阅 margins 、bottomMargin 、leftMargin 以及rightMargin 。
topRightRadius : real
右上角的圆角半径。如果未设置,则默认值为radius 。
另请参阅 radius 、topLeftRadius 、bottomLeftRadius 以及bottomRightRadius 。
visible : bool
该委托是否可见。默认值为true 。
注意:尽管 默认值为true ,但 fallback style (该规范覆盖了许多默认值)将background.visible 设置为false ,适用于通常不应绘制背景的控件,例如CheckBox 、RadioButton 和Slider 。
当visible 的值为false 时,delegate 将不会被创建。若需要创建隐藏的委托,请考虑将opacity 改为0 。
另请参阅 opacity 。
width : real
此委托的宽度。
注意: 仅当fillWidth 为false 时,此 属性才会设置委托的宽度。当fillWidth 在样式层次结构中更靠上的位置(例如在fallback style 中)被设置时,此规则同样适用。
如果该委托位于控件的根节点——例如background 或indicator ——则在计算控件的implicit width 时,它也会被包含在内,即使fillWidth 的值是true 也是如此。
另请参阅 height 、fillWidth 、minimumWidth 以及StyleKit 属性解析。
© 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.