[Interest] QML required property in view delegate - bug?

Benjamin B (BBenj) balga.benjamin at gmail.com
Fri May 13 14:10:51 CEST 2022


Hello,

I encountered a "friction point", and I think a bug, in QML with the
"required" attribute for properties, when used in a view delegate.
Here is an example hacked from the docs :

Item {
    ListModel {
        id: myModel
        ListElement { type: "Dog"; age: 8; noise: "meow" }
        ListElement { type: "Cat"; age: 5; noise: "woof" }
    }

    component SomeDelegate: Item {
        required property int age
        property string text
    }

    Repeater {
        model: myModel
        delegate: SomeDelegate {
            text: model.noise
        }
    }
}

Which raises the error "ReferenceError: model is not defined", as
documented. Is there a way around this?
I feel like this should be valid, as the delegate may not exactly match the
model in every case. I would prefer not having this "auto-bind" feature and
keep access to model/modelData/index.

But then, if I can't modify SomeDelegate (or want to keep the required
property), how do I do?
I tried various things to encapsulate SomeDelegate into something else, but
I always get the same error. As soon as some child item has a required
property, "model" is not available anymore.  This is a bug, right?

The following doesn't work for example (same error):

Item {
    ListModel {
        id: myModel
        ListElement { type: "Dog"; age: 8; noise: "meow" }
        ListElement { type: "Cat"; age: 5; noise: "woof" }
    }

    component SomeDelegate: Item {
        required property int age
        property string text
    }

    component AnotherDelegate: Item {
        property int _age
        property string _text

        SomeDelegate {
            age: 0
            text: ""
        }
    }

    Repeater {
        model: myModel
        delegate: AnotherDelegate {
            _age: model.age
            _text: model.noise
        }
    }
}

(I'm testing with qml/qmlscene, Qt 6.3.0, Qt 5.15.2)


Thank you,

--
Benjamin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220513/2ab1f971/attachment.htm>


More information about the Interest mailing list