Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

How to call setData of one C++ model from another C++ model?



  • hello, I have currently two C++ models. I would like to call setData method of C++ model from another C++ model. I also need to pass some reference doing so. I am confused how could I achieve this. Please help.


  • Moderators

    All you need - in principle - is a valid QModelIndex (item of that model), new data and role name. And access to the other model.

    But any details depend on how your code looks, whether it's C++ or QML etc. Please provide more info.



  • @sierdzio. I have larger model which holds reference to smaller model. Larger model is used in ListView. The checked listview items are used to create smaller model. The smaller model will also communicate with hardware to update its data. So, I need to pass references or values to create smaller model which is basically subset of larger model. The smaller model is used in GridView


  • Moderators

    Well, that is something. So, at the moment when you need to create the smaller model, you have selected items from big models. Now, you can add a method to your C++ model (make it Q_INVOKABLE so it's available from QML) which takes item indexes (or, if your items have some unique IDs, you can use them here, too). Use that method on QML side (I imagine you have some button that user will click when items are selected - that's where this method should be used).

    Now, in the C++ method, create a new small model and send it to QML via signal. Or, if you already have the small model, just send the data to it and make sure proper signals are emitted (beginInserRows(), beginResetModel(), dataChanged() etc. depending on how you do the update).


Log in to reply