Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    EXAMPLE: updatable model within a model

    Mobile and Embedded
    2
    6
    1146
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • QtHelex
      QtHelex last edited by QtHelex

      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 !

      0_1530013538416_Screenshot_2018-02-07-04-22-47.png!

      To avoid that others have to get thru the same painful process I've put a working example on gitHub:
      https://github.com/helex/subModelInModel

      In 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

      ekkescorner 1 Reply Last reply Reply Quote 1
      • ekkescorner
        ekkescorner Qt Champions 2016 @QtHelex last edited by

        @QtHelex thx providing this. will take a look at your solution later.

        ekke ... Qt Champion ... mobile business apps
        5.15 --> 6.6 https://t1p.de/ekkeChecklist

        QtHelex 1 Reply Last reply Reply Quote 0
        • QtHelex
          QtHelex @ekkescorner last edited by

          @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

          testModel->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 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.

          ekkescorner 1 Reply Last reply Reply Quote 0
          • ekkescorner
            ekkescorner Qt Champions 2016 @QtHelex last edited by

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

            ekke ... Qt Champion ... mobile business apps
            5.15 --> 6.6 https://t1p.de/ekkeChecklist

            QtHelex 1 Reply Last reply Reply Quote 0
            • QtHelex
              QtHelex @ekkescorner last edited by

              @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. :-)

              1 Reply Last reply Reply Quote 0
              • QtHelex
                QtHelex last edited by

                Okay, got it working using QQmlObjectListModel<>* instead of QQmlObjectListModelBase*.

                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! :-)

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post