ListElement 字段作为属性?
这是我的代码:
import QtQuick 1.0
ListModel {
property real firstValue: 2
property real secondValue: 3
property real thirdValue: 1
id: leftGrid
ListElement {
icon: "Images/1.png"
value: leftGrid.firstValue
}
ListElement {
icon: "2.png"
value: -1
}
ListElement {
icon: "3.png"
value: leftGrid.secondValue
}
ListElement {
icon: "4.png"
value: leftGrid.thirdValue
}
}
这给了我错误:
ListElement: cannot use script for property value
我应该做什么?
Here is my code :
import QtQuick 1.0
ListModel {
property real firstValue: 2
property real secondValue: 3
property real thirdValue: 1
id: leftGrid
ListElement {
icon: "Images/1.png"
value: leftGrid.firstValue
}
ListElement {
icon: "2.png"
value: -1
}
ListElement {
icon: "3.png"
value: leftGrid.secondValue
}
ListElement {
icon: "4.png"
value: leftGrid.thirdValue
}
}
This gives me the error:
ListElement: cannot use script for property value
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ListElement
字段不是传统意义上的 QML 属性,而是其父ListModel
中的角色。所以属性绑定在这种情况下不起作用。这就是为什么您会收到 QML 错误:“ListElement:无法使用脚本获取属性值”,这有点令人困惑。尽管已向 Qt 团队提交了修复此问题的请求,但迄今为止尚未实施任何解决方案。
同时,如果您不想实现自己的 C++ 类来处理列表模型,正如 @Luca Carlon 所建议的那样,我已经尝试过一个有效的 QML 解决方法/解决方案。要点是:
ListModel.append()
进行模型初始化,并使用ListModel.setProperty()
进行模型更新。在这两个方法调用的参数列表中,可以将角色分配给其他属性,这相当于属性绑定。将此 QML 解决方法/解决方案应用于原始示例代码将产生以下修订后的示例代码:
如果您运行上述修订后的示例代码(例如使用 Qt 5.5.0),您将不再收到 QML 错误。当然,我也不喜欢这个 QML 解决方法/解决方案。
The
ListElement
fields are not QML properties in the traditional sense, but rather the roles in its parentListModel
. So property binding does not work in this case. That is why you get QML error: "ListElement: cannot use script for property value", which is a bit confusing.Although there has been a request submitted to the Qt Team to have this fixed, so far no resolution has been implemented.
In the meantime, if you do not want to implement your own C++ class to handle the list model, as suggested by @Luca Carlon, there is a QML workaround/solution I have tried which works. The main points are:
ListModel
when its fields need to reference other properties.ListModel.append()
for the model initialization, and useListModel.setProperty()
for the model updating. Inside the argument list of these 2 method calls, the roles can be assigned to the other properties, which is equivalent to property binding.Applying this QML workaround/solution to your original example code will result in the following revised example code:
If you run the above revised example code, using Qt 5.5.0 for example, you will no longer get the QML error. Sure, I do not like this QML workaround/solution either.
我也有这个问题。考虑采用此处报告的解决方案之一:https://bugreports.qt.io/browse/ QTBUG-16289 根据您的 Qt 版本进行解释。
我个人更喜欢用 C++ 实现该模型。阅读此内容: http:// /cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html。
I had this problem as well. Consider adopting one of the solutions reported in here: https://bugreports.qt.io/browse/QTBUG-16289 according to your version of Qt as explained.
I personally prefer to implement the model in C++. Read this: http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html.
我的解决方案是不使用
ListElement
作为模型项:My solution to this is not using
ListElement
as model item:如果您尝试从 C++ 引用未使用 Q_ENUM 注册的枚举,也可能会发生此错误。
This error can also occur if you try to reference an enum from C++ that is not registered with Q_ENUM.
这也可能是另一种解决方法,因为 "从 Qt 5.11 ListElement 也开始允许将函数声明分配给角色。这允许定义具有可调用操作的 ListElements。”
This also could be another workaround since "Beginning with Qt 5.11 ListElement also allows assigning a function declaration to a role. This allows the definition of ListElements with callable actions."