Important: Please read the Qt Code of Conduct -

QList<QObject *> data model roles in QML. Ok in example and not Ok in real life.

  • That is my QML code instantly,

    ComboBox {
            textRole: "categoryName"
            model: converter.categoryModel
            onCurrentIndexChanged : {

    ComboBox sees "categoryName" and shows data, but when I try to access any other role ("") it can not access in any modification of this string.
    Here are properties:

    Q_PROPERTY(Category category READ category WRITE setCategory NOTIFY categoryChanged)
        Q_PROPERTY(double coefficient READ coefficient WRITE setCoefficient NOTIFY coefficientChanged)
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(QString categoryName READ categoryName WRITE setCategoryName NOTIFY categoryNameChanged)

    TypeError: Cannot read property 'name' of undefined
    what is wrong?

    Example copy-past:

    ListView {
        width: 100; height: 100
        model: myModel
        delegate: Rectangle {
            height: 25
            width: 100
            color: model.modelData.color
            Text { text: name }

    and in this case when MyModel is registered with qqmlcontext property let access to all the roles this way.
    So, again, what is wrong with my code?

  • Moderators

    @Kofr AFAIK It works in ListView's delegate only because properties of model are made available for it. It won't work outside of the delegate and thus in case of ComboBox modelData wont work.
    To access the model data you can use get (for ListModel ) and passing it in your case currentIndex. For C++ model you can write your own Q_INVOKABLE function.

Log in to reply