Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. accessing elements of a ListModel

accessing elements of a ListModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
27 Posts 3 Posters 8.0k Views 2 Watching
  • 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.
  • fcarneyF fcarney

    Are you taking data from 2 sources? If so you can build up a ListModel and combine multiple sources of data using a function. Then use that model as the model for your repeater.

    The set function of ListModel takes a js object as input. Any time the lists change you can rebuild that list which will automatically update all the Bottles in the repeater.

    mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #17

    @fcarney said in accessing elements of a ListModel:

    Are you taking data from 2 sources?

    Yes I am: I have the positions and dimensions in a QML ListModel. The "real" information about the bottle (size, contents, expiration date, etc) comes from a view model.

    If so you can build up a ListModel and combine multiple sources of data using a function. Then use that model as the model for your repeater.

    I like the sound of this. How do I put this in a loop, so I don't have to do something like this?

      ListModel {
        id: bottleModel_19
    
        ListElement {
          // position 1
          x: 407
          y: 18
          height: 78
          width: 78
          bottleLabel: (reagentManager.bottleList[index] !== undefined)
                       ? reagentManager.bottleList[index].name
                       : "ETH"
        }
        ...
    
    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #18

      No, use the methods for ListModel like insert.

      onChangeOfSourceList: {
        bottleModel_19.clear()
        for(var count=0; count<x; ++count){ 
          // insert, get, and set use js objects
          bottleModel_19.insert(count, {
            "x":list1[count].whatever,
            ...  // repeat for items from both lists
          })
        }
      }
      

      Edit: Maybe append is better. Also clear list each time you recreate the list.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      1
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #19

        Does onChangeOfSourceList correspond to the signal I emit when I've updated the list? Here's my property:

          Q_PROPERTY(Bottles bottleList
                         MEMBER m_bottleList
                             NOTIFY bottleListChanged)
        

        I tried onbottleListChanged, but this isn't correct.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #20

          Yes, something that emits a signal that you are calling when you update lists.
          It will be:

          onBottleListChanged
          

          Notice the B gets capitalized. Is there another signal that can trigger this too? Then you will need to create a function that gets called in each instance.

          function updateListModel(){
            ...
          }
          onBottleListChanged: updateListModel()
          onOtherListChanged: updateListModel()
          

          C++ is a perfectly valid school of magic.

          mzimmersM 1 Reply Last reply
          0
          • fcarneyF fcarney

            Yes, something that emits a signal that you are calling when you update lists.
            It will be:

            onBottleListChanged
            

            Notice the B gets capitalized. Is there another signal that can trigger this too? Then you will need to create a function that gets called in each instance.

            function updateListModel(){
              ...
            }
            onBottleListChanged: updateListModel()
            onOtherListChanged: updateListModel()
            
            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #21

            @fcarney to which component is the onBottleListChanged: applied? I've tried putting this in several places in my QML file, and the editor warns of an invalid property name. Am I supposed to put this in a Connections object?

            I don't have another signal for a bottle list update. Earlier you asked:

            Are you taking data from 2 sources?

            Yes I am. I put some data in the QML ListModels. This is data pertaining to the UI itself (screen location of the representation, size, etc.) "Real" data about the bottles (label, contents, fill level) is kept in a C++ object. This seemed like a natural breakdown for the data; is using two sources a bad idea?

            Thanks...

            1 Reply Last reply
            0
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #22

              @mzimmers said in accessing elements of a ListModel:

              Q_PROPERTY(Bottles bottleList
              MEMBER m_bottleList
              NOTIFY bottleListChanged)

              What object has this in it?

              C++ is a perfectly valid school of magic.

              mzimmersM 1 Reply Last reply
              0
              • fcarneyF fcarney

                @mzimmers said in accessing elements of a ListModel:

                Q_PROPERTY(Bottles bottleList
                MEMBER m_bottleList
                NOTIFY bottleListChanged)

                What object has this in it?

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #23

                @fcarney

                class ReagentManager : public QObject {
                  Q_OBJECT
                 public:
                  Q_PROPERTY(Bottles bottleList
                                 MEMBER m_bottleList
                                     NOTIFY bottleListChanged)
                ...
                class ChangeConsumables : public QObject {
                  Q_OBJECT
                  ReagentManager m_reagentManager;
                ...
                }
                engine->rootContext()->setContextProperty("reagentManager", &m_reagentManager);
                
                1 Reply Last reply
                0
                • fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by
                  #24

                  @mzimmers said in accessing elements of a ListModel:

                  engine->rootContext()->setContextProperty("reagentManager", &m_reagentManager);

                  Then its a connection on the reagentManager.

                  C++ is a perfectly valid school of magic.

                  mzimmersM 1 Reply Last reply
                  0
                  • fcarneyF fcarney

                    @mzimmers said in accessing elements of a ListModel:

                    engine->rootContext()->setContextProperty("reagentManager", &m_reagentManager);

                    Then its a connection on the reagentManager.

                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #25

                    @fcarney the reagentManager generates the signal, but who consumes it?

                    fcarneyF 1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      @fcarney the reagentManager generates the signal, but who consumes it?

                      fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #26

                      onBottleListChanged: updateListModel()

                      You call the function that rebuilds your ListModel. Don't you want that to update when it changes? Also, you said you had two lists of data. Does it have a signal too?

                      C++ is a perfectly valid school of magic.

                      mzimmersM 1 Reply Last reply
                      0
                      • fcarneyF fcarney

                        onBottleListChanged: updateListModel()

                        You call the function that rebuilds your ListModel. Don't you want that to update when it changes? Also, you said you had two lists of data. Does it have a signal too?

                        mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by
                        #27

                        @fcarney said in accessing elements of a ListModel:

                        onBottleListChanged: updateListModel()

                        Oh, OK...I see my problem now. I was trying to use this line of code from within the repeater. I see now that it should go in the containing Rectangle.

                        You call the function that rebuilds your ListModel. Don't you want that to update when it changes? Also, you said you had two lists of data. Does it have a signal too?

                        I have two sources of data: one in C++ and one in a QML ListModel. I actually have 2 QML ListModels, but only use one (which one to use is determined at runtime by an environment variable). The only signal is emitted by the C++ update function; nothing in the ListModels.

                        As it turns out, I've been able to eliminate the use of the QML update function entirely (though I still need the getColor routine() above, so I won't need to use this feature here, but this has been very educational. Thank you for all the help.

                        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