【发布时间】:2019-04-04 16:01:53
【问题描述】:
我对 QML 中的项目有疑问。我想得到一个项目的孩子,但它似乎在第一个元素中工作。
详细代码如下:
我有一个带有列表自定义组件 AAA_Styles 的 gridview
GridView{
id: grdViewDeviceControl
clip: true
interactive: true
ScrollIndicator.vertical: ScrollIndicator{}
cellWidth: 200
cellHeight: 300
model: ListModel{}
delegate: Item {
width: grdViewDeviceControl.cellWidth
height: grdViewDeviceControl.cellHeight
AAA_Styles{
id: deviceControl
objectName: "deviceControl"
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
name: Names
subname: Subnames
}
}
我的自定义AAA_RTS是一个QML组件,有一些功能,例如:
- function_a()
- 函数_b()
我使用
grdViewDeviceControl.model.append()
我确保模型有我添加的数据,因为它出现在我的设计中并且 gridview 的计数是 2 个元素
console.log(grdViewDeviceControl.count) //结果是2
之后,我尝试使用按钮的信号 onRelease 中的方法让每个元素访问它们可用的功能:
onReleased: {
console.log("number of item: " + grdViewDeviceControl.count)
var myRTS = grdViewDeviceControl.contentItem.children[0]
console.log(myRTS)
console.log(myRTS.children[0])
myRTS = grdViewDeviceControl.contentItem.children[1]
console.log(myRTS)
console.log(myRTS.children[1])
}
控制台上的结果:
qml:项目数:2
qml: QQuickItem(0x9828f60)
qml: AAA_Styles_QMLTYPE_0_QML_112(0x9829070, "deviceControl")
qml: QQuickItem(0x5554140)
qml: 未定义
使用第一个元素 grdViewDeviceControl.contentItem.children[0],我访问 function_a 或 function_b 成功但是当我使用第二个时出现错误
TypeError:无法调用未定义的方法“function_a”
那么谁能告诉我为什么我错了以及如何解决它?
非常感谢您的帮助!
【问题讨论】:
-
访问视图中的项目是一种不好的做法,因为它会带来诸如您指出的问题,而您应该与模型信息进行交互。为什么要访问这些项目?
-
是的,我不知道最好的方法。感谢您的推荐以及如何访问模型?我想我必须在 gridview 之外创建一个模型并通过 id 访问它
-
没错,检查我的旧答案stackoverflow.com/a/55318200/6622587
-
您指出的可能解决方案可以工作,但我没有分析它,因为对我来说这不是正确的解决方案,因为它难以维护,例如,如果您更改组件,您将不得不修改代码的另一部分。也就是说,您可能的解决方案是一个临时补丁,将来会给您带来很多麻烦(我在开始使用 QML 时已经拥有它们,我不想再拥有它们)。对我来说,你有一个XY problem,也就是说,你要的是一个没有人保证有效的答案,而不是你的潜在问题。
-
我认为 cmets 已经扩展了。我要说的最后一件事是,您首先要设计组件,然后再实现它们。很多时候我们都有编写代码的坏习惯,而没有清楚地了解我们想要什么(设计),请阅读What is the XY problem?bye
标签: qt qml qtquickcontrols2