Updating C++ Model Data from QML Listview



  • I have a textfield and checkbox in listview , Now i want to update model data when checkbox state changed. also textfiled should be editable when clicked so i should be able to update data in the C++ model
    CheckBox {
    anchors.right: parent.right
    checked: cFav
    onCheckedChanged:
    {
    // Model name is contactModel

        }
    }
    

    Please help me with this.



  • Assuming that contact model is exposed from c++ side. Expose some method as slot in your model, call that method and update your model data structure in backend



  • Thanks Dheerendra. Yes i have exposed the model as a context property. I agree with you that i can call a slot and pass index and value to update model. But my question is is there any way like, onCheckedChanged i can assign value to contactModel. I mean to say contactModel.setproperty(index, role, value). May be i am not correct my whole intention behind this is to make Models setData should be called directly from QML Listview



  • No direct way like this. Either you expose some property through q_property and assign the value. Internally in you model it will call set method. Or else you need go through exposed slot



  • Thanks for the clarification.



  • If you want your listview to be generic, you can call QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) from QML since it is a Q_INVOKABLE, the hard part is figuring out the role number.
    If your listview is tailor-made for a specific model, I'd recommend you to expose a specific functions in your model that you would call in your view.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.