[英]QML, GridView delegate: ReferenceError: `<variable_name>` is not defined
I am struggling with it a bit so far, but couldn't find an answer.到目前为止,我正在为此苦苦挣扎,但找不到答案。 Here is the core situation.
这是核心情况。
I have a GridView with ListModel:我有一个带有 ListModel 的 GridView:
GridView {
id: shapeSidePos
...
model: shapeSidePosList
delegate: ButtonIcon {
style: ButtonIcon.Styles.Parallel
icon: path
...
}
ListModel {
id: shapeSidePosList
ListElement { path: "../../images/shape_side_positioning_base.png" }
ListElement { path: "../../images/shape_side_positioning_right.png" }
...
}
}
And in a runtime I get error:在运行时我得到错误:
ReferenceError: path is not defined
The thing is, when I have used eg Rectangle
instead of ButtonIcon
, everything worked fine!问题是,当我使用
Rectangle
而不是ButtonIcon
时,一切正常! Accessing elements with model.path
did not work either.使用
model.path
访问元素也不起作用。 In ButtonIcon, icon
is defined so:在 ButtonIcon 中,
icon
的定义如下:
property alias icon: buttonIcon.source
where buttonIcon is an image with MouseArea.其中 buttonIcon 是带有 MouseArea 的图像。
Been searching through web, found some tricks with JS but that makes things complicated and many developers may use this code later.通过网络搜索,发现了一些 JS 技巧,但这使事情变得复杂,许多开发人员以后可能会使用此代码。 It would be problematic for them.
这对他们来说是有问题的。 However, I can access the right path while referring directly to an index:
但是,我可以在直接引用索引的同时访问正确的路径:
icon: shapeSidePosList.get(0).path
I tried packing delegate into Component, tried DelegateModel, nothing has given the expected result.我尝试将委托打包到组件中,尝试了 DelegateModel,但没有给出预期的结果。
I have no idea what is going on,我不知道发生了什么,
I would be very thankful for any help provided.我将非常感谢提供的任何帮助。
@iam_peter wrote: @iam_peter 写道:
You could try to enforce the path property in the delegate with the required keyword like shown here https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#view-delegates and see if it yields any other errors.
您可以尝试使用此处所示的必需关键字在委托中强制执行路径属性https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#view-delegates并查看它是否产生任何其他错误。
I have added a required
property to a delegate:我已经向委托添加了一个
required
的属性:
delegate: ButtonIcon {
required property var path // <-- this
required property int index // <-- and this
style: ButtonIcon.Styles.Parallel
icon: path
...
}
and it worked.它奏效了。 Thank you very much!
非常感谢你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.