Qt World Summit: Submit your Presentation

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:

    0_1530013529139_Screenshot_2018-02-07-04-22-54.png !


    To avoid that others have to get thru the same painful process I've put a working example on gitHub:

    In case someone has a idea how to make it better, more performant or elegant please contribute or drop me a message.


    Update: added a way to access and update single elements at the model within the model

  • Qt Champions 2016

    @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 using


    I'm also able to get the pointer of the submodel using:


    But since the submodel has to be a QQmlObjectListModelBase* and not QQmlObjectListModel<MySubmodel>* there is no at(x) function to access each row of MySubmodel.

    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.

  • Qt Champions 2016

    @QtHelex do you also have to add and remove data to/from your submodel ?

  • @ekkescorner

    @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 of QQmlObjectListModelBase*.

    This was missing in my constructor of the app Object:
    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! :-)

Log in to reply