qqmlcomponent - QML如何通过单击listView代表内定义的复选框来获取ListView中的索引号?
问题描述
- 好吧,我正在尝试在 listView 中创建一个包含两个文本和一个复选框的项目列表。
- 我想在选中复选框时添加商品的价格。
- 要做到这一点,我需要知道 listView 的项目的索引,以便我可以获得它的“价格”并存储数据。
- 我无法通过复选框获取列表的当前索引,因为它表现为单个实体。
- 但是,如果我放置鼠标区域,我可以获得索引,但我需要通过复选框来获取它。
以供参考:
Component { // delegate
id:_delegate
Rectangle{
width: 100
height: 50
color: "beige"
border.color: "black"
Text {
id: _textid
text: name
}
Text {
id: _textid2
anchors.top: _textid.bottom;anchors.topMargin: 5
anchors.left: _textid.left
text: price
}
CheckBox {
id:_checkbox
anchors.top:parent.top;anchors.topMargin: 5
anchors.left: parent.left;anchors.leftMargin: 50
}
}
解决方案
您可以使用 onCheckedChanged 事件,然后从模型中获取当前索引的价格,如下所示:
Component { // delegate
id:_delegate
Rectangle{
width: 100
height: 50
color: "beige"
border.color: "black"
Text {
id: _textid
text: name
}
Text {
id: _textid2
anchors.top: _textid.bottom;anchors.topMargin: 5
anchors.left: _textid.left
text: price
}
CheckBox {
id:_checkbox
anchors.top:parent.top;anchors.topMargin: 5
anchors.left: parent.left;anchors.leftMargin: 50
onCheckedChanged: {
_textid2.text = _checkbox.checked ? your_model.get(index).price : ""
}
}
}
推荐阅读
- android - 如何使`this`在一个范围内引用 Kotlin Android Extension 类型类?
- android - 我使用了 distributionUrl =https\://services.gradle.org/distributions/gradle-6.1.1-all.zip 但同步后布局文件仍然丢失
- git - 将 git hub 存储库更改为主文件夹(iso 子文件夹)
- mysql - 语法不正确的 SQL 不会在 MariaDB 服务器版本中引发错误:10.1.45?
- javascript - [Vue 警告]:beforeCreate 钩子中的错误:“ReferenceError:文档未定义”
- c# - 将带有 IO 操作的无限循环转换为可观察的
- redux - 在 redux-thunk 操作后使用 axios 进行承诺
- reactjs - 需要说明:使用 useEffect 钩子从 API 获取数据并获取名称
- angular - 如何使用来自服务的值进行角度的@Hostbinding?
- python - 如何从实时视频中获取和堆叠帧并使用 LSI 算法