gpt4 book ai didi

qt - 通用 QML 委托(delegate),但非通用 View 附加属性?

转载 作者:行者123 更新时间:2023-12-04 10:19:06 25 4
gpt4 key购买 nike

View 将属性附加到委托(delegate)。对于 ListView,委托(delegate)可以访问 ListView.view.widthListView.isCurrentItem 属性:

Rectangle {
width: ListView.view.width
height: 40

color: ListView.isCurrentItem ? "gray" : "lightGray"

Text {
anchors.centerIn: parent
text: index
}
}

通过类型名称引用 View ,委托(delegate)似乎失去了它的通用性。

如果我想在 GridView 中使用同一个委托(delegate)怎么办?

最佳答案

您应该从您的委托(delegate)创建一个组件,并在其实例化期间设置 property isCurrentItem。换句话说,创建新的 qml 文件并将其命名为例如“Delegate.qml”并添加property bool isCurrentItem:

import QtQuick 2.4

Rectangle {
property bool isCurrentItem: false
width: parent.width
height: 20
color: isCurrentItem ? "gray" : "lightGray"
Text {
anchors.centerIn: parent
text: index
}
}

你可以像这样在 ListView 中使用它:

ListView {
model: 10
width: 40
height: 200
delegate: Delegate {
isCurrentItem: ListView.isCurrentItem
}
}

在 GridView 中类似:

GridView {
model: 10
width: 40
height: 200
delegate: Delegate {
isCurrentItem: ListView.isCurrentItem
}
}

你可以用同样的方式提供 ListView/GridViewwidth 来委托(delegate),但在这种情况下 parent.width 也将按照您想要的方式工作。

关于qt - 通用 QML 委托(delegate),但非通用 View 附加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com