Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. EXAMPLE: updatable model within a model
QtWS25 Last Chance

EXAMPLE: updatable model within a model

Scheduled Pinned Locked Moved Mobile and Embedded
6 Posts 2 Posters 1.6k Views
  • 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.
  • QtHelexQ Offline
    QtHelexQ Offline
    QtHelex
    wrote on last edited by QtHelex
    #1

    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

    ekkescornerE 1 Reply Last reply
    1
    • QtHelexQ 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

      ekkescornerE Offline
      ekkescornerE Offline
      ekkescorner
      Qt Champions 2016
      wrote on last edited by
      #2

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

      ekke ... Qt Champion 2016 | 2024 ... mobile business apps
      5.15 --> 6.8 https://t1p.de/ekkeChecklist
      QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

      QtHelexQ 1 Reply Last reply
      0
      • ekkescornerE ekkescorner

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

        QtHelexQ Offline
        QtHelexQ Offline
        QtHelex
        wrote on last edited by
        #3

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

        ekkescornerE 1 Reply Last reply
        0
        • QtHelexQ QtHelex

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

          ekkescornerE Offline
          ekkescornerE Offline
          ekkescorner
          Qt Champions 2016
          wrote on last edited by
          #4

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

          ekke ... Qt Champion 2016 | 2024 ... mobile business apps
          5.15 --> 6.8 https://t1p.de/ekkeChecklist
          QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

          QtHelexQ 1 Reply Last reply
          0
          • ekkescornerE ekkescorner

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

            QtHelexQ Offline
            QtHelexQ Offline
            QtHelex
            wrote on last edited by
            #5

            @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
            0
            • QtHelexQ Offline
              QtHelexQ Offline
              QtHelex
              wrote on last edited by
              #6

              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
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved