EXAMPLE: updatable model within a model
-
Hello forum!
I needed for a Android App a list of Pages which I need to create dynamically (except the first one). And each Page had to have a ListView on it.
Hence I needed a model for my ListView which is inside of a model for my Pages.
Google wasn't my friend and I needed a while and quite a lot of help of other community members to get it finally working:
!
!
To avoid that others have to get thru the same painful process I've put a working example on gitHub:
https://github.com/helex/subModelInModelIn case someone has a idea how to make it better, more performant or elegant please contribute or drop me a message.
Enjoy!
Update: added a way to access and update single elements at the model within the model
-
Hello forum!
I needed for a Android App a list of Pages which I need to create dynamically (except the first one). And each Page had to have a ListView on it.
Hence I needed a model for my ListView which is inside of a model for my Pages.
Google wasn't my friend and I needed a while and quite a lot of help of other community members to get it finally working:
!
!
To avoid that others have to get thru the same painful process I've put a working example on gitHub:
https://github.com/helex/subModelInModelIn case someone has a idea how to make it better, more performant or elegant please contribute or drop me a message.
Enjoy!
Update: added a way to access and update single elements at the model within the model
@QtHelex thx providing this. will take a look at your solution later.
-
@QtHelex thx providing this. will take a look at your solution later.
@ekkescorner Thanks a lot. I've thought it's worth sharing. :-)
First I had the opinion that this solution would fulfill my needs, but now the question raised: How to update a element of the model within the model?
I'm able to access and change each element of my
testModel
at example usingtestModel->at(z)->set_name("test");
I'm also able to get the pointer of the submodel using:
testModel->at(z)->get_submodel();
But since the submodel has to be a
QQmlObjectListModelBase*
and notQQmlObjectListModel<MySubmodel>*
there is noat(x)
function to access each row ofMySubmodel
.At my current project I have to update a few elements after the List got already populated, since I get those values just after a few seconds... and from that point on every few minutes a value changes depending on what the user does. Clearing and reloading the whole list is not really a option. All Ideas are welcome.
-
@ekkescorner Thanks a lot. I've thought it's worth sharing. :-)
First I had the opinion that this solution would fulfill my needs, but now the question raised: How to update a element of the model within the model?
I'm able to access and change each element of my
testModel
at example usingtestModel->at(z)->set_name("test");
I'm also able to get the pointer of the submodel using:
testModel->at(z)->get_submodel();
But since the submodel has to be a
QQmlObjectListModelBase*
and notQQmlObjectListModel<MySubmodel>*
there is noat(x)
function to access each row ofMySubmodel
.At my current project I have to update a few elements after the List got already populated, since I get those values just after a few seconds... and from that point on every few minutes a value changes depending on what the user does. Clearing and reloading the whole list is not really a option. All Ideas are welcome.
@QtHelex do you also have to add and remove data to/from your submodel ?
-
@QtHelex do you also have to add and remove data to/from your submodel ?
@ekkescorner said in update a model within a model?:
@QtHelex do you also have to add and remove data to/from your submodel ?
Yes
->append()
and->clear()
works already flawlessly.What is missing is the higher level stuff like
->at(x)
to find out the index of the row with the corresponding subID or to change single values.But I guess I've just found the issue, but broke the way to add new items dynamically. Will have to play a little bit more with it. :-)
-
Okay, got it working using
QQmlObjectListModel<>*
instead ofQQmlObjectListModelBase*
.This was missing in my constructor of the app Object:
qRegisterMetaType<QQmlObjectListModel<MySubmodel>*>("QQmlObjectListModel<MySubmodel>*");
No idea why since I needed so far only to register MetaTypes in case I would use it in Signals and Slots... but if it works you should sometimes stop asking. ;-)I've just updated the gitHub example. Have a look, I'm sure it could get useful at larger projects. IMHO a lot easier to handle using models instead of creating and destroying pages dynamically within qml.
Have a great weekend ekke! :-)