QML ListView 中为什么不能直接使用 style 属性
在 QML 中,ListView 是一个容器,它会自动为其子项创建对应的项视图。因此,ListView 中的子项通常需要使用 'delegate' 属性来定义其外观和行为,而不是使用 'style'。
如果你想要为 ListView 中的子项应用样式,你可以在 'delegate' 属性中使用样式。例如:
ListView {
model: myModel
delegate: Rectangle {
width: parent.width
height: 30
color: 'white'
Text {
text: model.display
font.pixelSize: 20
color: 'black'
anchors.centerIn: parent
}
style: 'background-color: gray; border: 1px solid black;'
}
}
在上面的例子中,我们为 ListView 的 'delegate' 属性设置了一个 Rectangle,并在其中定义了一个 Text 组件。我们还为 Rectangle 应用了一个样式,它定义了背景颜色和边框。这样,我们就可以为 ListView 中的子项应用样式了。
原文地址: https://www.cveoy.top/t/topic/nRGA 著作权归作者所有。请勿转载和采集!