在本页

StyledItem QML Type

渲染DelegateStyle... 更多...

Import Statement: import Qt.labs.StyleKit
Inherits:

Item

属性

详细描述

StyledItem 用于渲染DelegateStyle 。它读取color,border,gradient, 和image 等属性,并在内部创建相应的可视化元素。

如果DelegateStyle 上设置的delegatenull (默认值),则将自动使用 StyledItem 进行渲染。

自定义delegate 可以设置为任何 Item。但与 StyledItem 相结合,就可以在添加叠加、底层或着色器效果的同时保留默认的呈现方式。以下代码段展示了如何在默认委托之上绘制额外的 Item:

Style {
    component Star : Shape {
        id: star
        property color color
        ShapePath {
            fillColor: star.color
            scale: Qt.size(star.width, star.height)
            PathMove { x: 0.50; y: 0.00 }
            PathLine { x: 0.59; y: 0.35 }
            PathLine { x: 0.97; y: 0.35 }
            PathLine { x: 0.66; y: 0.57 }
            PathLine { x: 0.78; y: 0.91 }
            PathLine { x: 0.50; y: 0.70 }
            PathLine { x: 0.22; y: 0.91 }
            PathLine { x: 0.34; y: 0.57 }
            PathLine { x: 0.03; y: 0.35 }
            PathLine { x: 0.41; y: 0.35 }
            PathLine { x: 0.50; y: 0.00 }
        }
    }

    button {
        background.delegate: StyledItem {
            width: parent.width
            height: parent.height
            // Draw a star on top the default rendering
            Star {
                anchors.fill: parent
                color: "gold"
            }
        }
    }
}

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

有关覆盖、底层和着色器效果的更多示例,请参阅StyleKit 示例

另请参阅 DelegateStyle,delegate, 和data

属性文档

delegateStyle : DelegateStyle

该项目渲染的DelegateStyle

此属性为必填项。当StyledItemdelegate 的根项目时,它会自动设置。但在自定义委托中作为子项使用时,必须显式设置。

下面的代码段使用了一个自定义委托,在默认滑块手柄下方绘制了一颗星。由于根项目不是StyledItem ,因此它声明了一个必填属性delegateStyle (该属性已自动分配),并将其转发给子代StyledItem

Style {
    component Star : Shape {
        id: star
        property color color
        ShapePath {
            fillColor: star.color
            scale: Qt.size(star.width, star.height)
            PathMove { x: 0.50; y: 0.00 }
            PathLine { x: 0.59; y: 0.35 }
            PathLine { x: 0.97; y: 0.35 }
            PathLine { x: 0.66; y: 0.57 }
            PathLine { x: 0.78; y: 0.91 }
            PathLine { x: 0.50; y: 0.70 }
            PathLine { x: 0.22; y: 0.91 }
            PathLine { x: 0.34; y: 0.57 }
            PathLine { x: 0.03; y: 0.35 }
            PathLine { x: 0.41; y: 0.35 }
            PathLine { x: 0.50; y: 0.00 }
        }
    }

    slider.handle.delegate: Item {
        required property DelegateStyle delegateStyle

        implicitWidth: delegateStyle.implicitWidth
        implicitHeight: delegateStyle.implicitHeight
        width: parent.width
        height: parent.height
        scale: delegateStyle.scale
        rotation: delegateStyle.rotation
        visible: delegateStyle.visible

        // Draw a star underneath the default handle delegate
        Star {
            width: parent.width * 2
            height: parent.height * 2
            anchors.centerIn: parent
            color: "gold"
        }

        StyledItem {
            delegateStyle: parent.delegateStyle
        }
    }
}

另请参阅 DelegateStyledelegatedata

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