This behavior surprised me first time I encountered it as well, but it is not a 
bug as the behavior is documented here:

https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html
{required properties} can be used. If a delegate contains required properties, 
the named roles are not provided. Instead, the QML engine will check if the 
name of a required property matches that of a model role. If so, that property 
will be bound to the corresponding value from the model.

So if you have any “required” keyword in the delegate, the model won’t be 
attached.

Even Kristoffersen
even.kristoffer...@semcon.com <mailto:even.kristoffer...@semcon.com>
+4795223753

From: Interest <interest-boun...@qt-project.org> On Behalf Of Benjamin B (BBenj)
Sent: fredag 13. mai 2022 14:11
To: interest@qt-project.org
Subject: [Interest] QML required property in view delegate - bug?

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




When you communicate with us or otherwise interact with Semcon, we will process 
personal data that you provide to us or we collect about you, please read more 
in our Privacy Policy<https://semcon.com/data-privacy-policy/>.
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to