PDA

View Full Version : C++ class with multiple QAbstractListModel?



benberli
8th September 2016, 18:54
I have several C++ models implemented using QAbstractListModel and showing these in QML using Repeaters.
Every of the C++ QAbstractListModel derived classes is registered independently and instantiated in QML.

What I would like to have is one C++ class with multiple models that I show in QML. That is since I would like to implement in C++ cross-model logic that needs to have access to all the models data and manipulate them accordingly.
I currently do not have this connection in my design.

Appreciate if someone could point me into the right direction.

Thanks,
Ben

d_stranz
8th September 2016, 21:20
It only makes sense if the models are each a different subset of the same data (eg. each model is the result of searching using a different key) or if the models are different data linked by a common key (eg. user name and address info, the items a user has bought, the set of individual purchases by that user. "user" is the common key).

In the first type, you decide how you concatenate the models - if one model has 20 rows and the next has 34, then your "pseudo-model" reports it has 54 rows and returns information from rows 0 - 19 from the first model, and from the second model for rows 20 - 53.

In the second type, it is more complex. Your pseudo-model will have as many columns as all models combined, and will have columns that repeat for several rows (eg. name and address will repeat for each item purchased entry for that user).

How you do this will depends on the specific details.

benberli
9th September 2016, 07:48
Thanks for your reply d_stranz.

To make it more specific -
Let's assume the application should give the user the option to add rectangles, circles and triangles from the QML GUI.
Currently I have 3 QAbstractListModel classes, one for rectangles, one for circles and one for triangles. Each of the types is displayed by a Repeater module in QML running over the QAbstractListModel with the correct delegate.

I would like to have another C++ "master class" that can have access to all of the 3 lists to manipulate the data.

Thanks,
Ben

wysota
9th September 2016, 08:44
I would like to have another C++ "master class" that can have access to all of the 3 lists to manipulate the data.

So why don't you do exactly that? Have an object which keeps pointers to all the models and manipulates them according to your wishes.

benberli
9th September 2016, 13:24
So why don't you do exactly that? Have an object which keeps pointers to all the models and manipulates them according to your wishes.

Well, just not sure how to technically achieve it.
I have currently 3 classes deriving from QAbstractModel. Their instantiation is done in QML separately.
Not sure how to create a 4th class and pass the pointers to it .. I mean, how do I get an object pointer from QML and pass it to a C++ class?
Another option is maybe to create a class where the QAbstractModels are children of this class. But then I don't know how in QML to access the children and pass as models to different repeaters.

Sorry, I am sure this is basics..

anda_skoa
9th September 2016, 14:55
You create a class that has three properties, one for each model.
The type of the property can be the pointer type of the specific model class or just "QAbstractItemModel*"

If the class creates the models in C++, these properties can be read-only and CONSTANT, if you want to create the model instances in QML then they are read-write with a NOTIFY signal.

Given your description of your data (geometric shapes), you could potentially also do a model that contains all items and has an additional role for the type of shape, using that to select delegates for a single repeater.

Cheers,
_