Nominate our 2022 Qt Champions!

QAbstractListModel: connect item's signal to the dataChanged signal of the model

  • Hi,

    I have subclassed QAbstractListModel for exposing a list of my objects to QML (using a ListView, there).
    I know that I can change the properties of items by overriding setData in my class, but that works from QML when I assign something to the corresponding role:
    @void setData(QModelIndex index, QVariant value, Role role)
    switch (role)
    case NameRole:
    emit dataChanged(index, index, { NameRole });
    and from QML, in the delegate of the ListView:
    @Item {
    Button { onClicked: name = "randomString"; }

    Now, I want to open a popup with a few controls that will let me choose a value for one of the properties, and a button for confirmation. I tried passing to the popup a pointer to the element (say, I got a MyClassModel, and the popup stores a @property MyClass item@), but invoking the setter, for that property, directly on the element doesn't trigger the dataChanged event on the model, and the view remains unaltered.

    For now I have added a function to my model:
    @void set_property(int i, QString name, QVariant value)@
    where I manually set the property called name of the item with index i and then emit dataChaged; however, I was wondering if there's a better solution from QML, something that allows me to write inside the popup, which is a separate QML item (file), myproperty = newValue and have the system automatically call the underlying model's setData function; or, maybe in the model's append function, from C++, I can manually connect each of the items' propertyChanged signal to a slot in the model which will in turn emit dataChanged signal for the corresponding row...

    I think, and I certainly hope, I've been clear enough and that someone will help :)

  • Up please

  • I guess this is a common case. Any answers for this question?

Log in to reply