问题 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

我该怎么办?


10207
2017-10-05 09:46


起源



答案:


我也有这个问题。考虑采用此处报告的解决方案之一: https://bugreports.qt.io/browse/QTBUG-16289 根据您的Qt版本解释。

我个人更喜欢用C ++实现该模型。读这个: http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html


6
2017-10-06 06:59



确实做到了,但我完成了!谢谢 - Guillaume Slashy
链接坏了 - Ispas Claudiu
新链接是: bugreports.qt.io/browse/QTBUG-20631 但错误报告已关闭并替换为 bugreports.qt.io/browse/QTBUG-16289 - jonathanzh


答案:


我也有这个问题。考虑采用此处报告的解决方案之一: https://bugreports.qt.io/browse/QTBUG-16289 根据您的Qt版本解释。

我个人更喜欢用C ++实现该模型。读这个: http://cdumez.blogspot.com/2010/11/how-to-use-c-list-model-in-qml.html


6
2017-10-06 06:59



确实做到了,但我完成了!谢谢 - Guillaume Slashy
链接坏了 - Ispas Claudiu
新链接是: bugreports.qt.io/browse/QTBUG-20631 但错误报告已关闭并替换为 bugreports.qt.io/browse/QTBUG-16289 - jonathanzh


ListElement 字段不是传统意义上的QML属性,而是其父级中的角色 ListModel。因此,在这种情况下,属性绑定不起作用。这就是为什么你得到QML错误:“ListElement:不能使用脚本作为属性值”,这有点令人困惑。

尽管已经向Qt团队提交了修复此请求的请求,但到目前为止还没有实施任何解决方案。

与此同时,如果您不想实现自己的C ++类来处理列表模型,正如@Luca Carlon所建议的那样,我尝试过的QML解决方法/解决方案是有效的。要点是:

  • 不使用 ListModel 当其字段需要引用其他属性时。
  • 相反,使用 ListModel.append() 用于模型初始化,并使用 ListModel.setProperty() 用于模型更新。在这两个方法调用的参数列表中,可以将角色分配给其他属性,这相当于属性绑定。

将此QML变通方法/解决方案应用于原始示例代码将生成以下修订示例代码:

import QtQuick 2.0

Rectangle {
    width: 400
    height: 200

    ListModel {
        property real firstValue: 2
        property real secondValue: 3
        property real thirdValue: 1
        id: leftGrid

    // The commented-out code belowwon't work:
    // ListElement:Cannot use script for property value.
    //        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
    //        }
    // QML workaround/solution to the above code:

        // 1. Initialize the list model:
        property bool completed: false
        Component.onCompleted: {
            append({"icon": "Images/1.png", value: leftGrid.firstValue});
            append({"icon": "2.png", value: -1});
            append({"icon": "3.png", value: leftGrid.secondValue});
            append({"icon": "4.png", value: leftGrid.thirdValue});
            completed = true;
        }

        // 2. Update the list model:
        onFirstValueChanged: {
            if(completed) setProperty(0, "value", leftGrid.firstValue);
        }
        onSecondValueChanged: {
            if(completed) setProperty(2, "value", leftGrid.secondValue);
        }
        onThirdValueChanged: {
            if(completed) setProperty(3, "value", leftGrid.thirdValue);
        }
    }
}

如果您运行上面修改过的示例代码,例如使用Qt 5.5.0,您将不再收到QML错误。当然,我也不喜欢这个QML解决方法/解决方案。


8
2017-10-16 01:25