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. QML & Array of Objects to QML side
QtWS25 Last Chance

QML & Array of Objects to QML side

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 4 Posters 3.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.
  • dheerendraD Offline
    dheerendraD Offline
    dheerendra
    Qt Champions 2022
    wrote on last edited by
    #1

    Hi

    I have array of Objects in C++ side. E.g QList<Fruits>. Assume that number of fruits are 5. I would like to display the name of fruit using five text elements.

    1. I can expose these objects using Model. If Expose the fruit list as model, I have to use them View.
    2. If I expose through QVariantList, any updates to fruit list will not be updated updated internally. Issues with this approach is that need to send the full list again.

    Is there way I can use the model, still update the data with model internally and use it without Views ?

    Please note that I'm experimenting with different options. If you have any inputs please suggest.

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    E D 2 Replies Last reply
    0
    • dheerendraD dheerendra

      Hi

      I have array of Objects in C++ side. E.g QList<Fruits>. Assume that number of fruits are 5. I would like to display the name of fruit using five text elements.

      1. I can expose these objects using Model. If Expose the fruit list as model, I have to use them View.
      2. If I expose through QVariantList, any updates to fruit list will not be updated updated internally. Issues with this approach is that need to send the full list again.

      Is there way I can use the model, still update the data with model internally and use it without Views ?

      Please note that I'm experimenting with different options. If you have any inputs please suggest.

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @dheerendra said in QML & Array of Objects to QML side:

      Is there way I can use the model, still update the data with model internally and use it without Views ?

      I didn't understand, can you rephrase the question? What do you mean by "update the data with model internally"? What is using without Views?

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        @Eeli-K
        Thank you responding to my question. I would like to have model in C++. Assume I have 5 elements in model and it has role called 'name'. I would like to display the name with simple 5 text elements instead of using ListView or GridView etc. Also I would like the text elements to updated whenever the model changes.

        Hope it clarifies.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        E 1 Reply Last reply
        0
        • dheerendraD dheerendra

          @Eeli-K
          Thank you responding to my question. I would like to have model in C++. Assume I have 5 elements in model and it has role called 'name'. I would like to display the name with simple 5 text elements instead of using ListView or GridView etc. Also I would like the text elements to updated whenever the model changes.

          Hope it clarifies.

          E Offline
          E Offline
          Eeli K
          wrote on last edited by Eeli K
          #4

          @dheerendra I think you still need something which uses a model automatically or you have to catch the signals from C++ and update the visual elements manually. Strictly speaking a Repeater is not a View but it's updated automatically:

          import QtQuick 2.7
          import QtQuick.Controls 2.2
          
          ApplicationWindow {
              visible: true
              width: 640
              height: 480
          
              ListModel {
                  id: lm
                  ListElement {}
                  ListElement {}
              }
          
              Row {
                    Repeater {
                        model: lm
                        Button {
                            text: lm.count
                            onClicked: lm.append({})
                        }
                    }
                }
          }
          
          
          1 Reply Last reply
          0
          • L Offline
            L Offline
            liubbc
            wrote on last edited by liubbc
            #5

            I think Signal & Slot may be helpful for update data

            1 Reply Last reply
            0
            • dheerendraD dheerendra

              Hi

              I have array of Objects in C++ side. E.g QList<Fruits>. Assume that number of fruits are 5. I would like to display the name of fruit using five text elements.

              1. I can expose these objects using Model. If Expose the fruit list as model, I have to use them View.
              2. If I expose through QVariantList, any updates to fruit list will not be updated updated internally. Issues with this approach is that need to send the full list again.

              Is there way I can use the model, still update the data with model internally and use it without Views ?

              Please note that I'm experimenting with different options. If you have any inputs please suggest.

              D Offline
              D Offline
              DBoosalis
              wrote on last edited by
              #6

              @dheerendra

              Do you really need a model in C++ to do this. Here is how I create a row of tabs dynamically in QML:

               TabView {
                      id:wayPointTabView
              
                      anchors.top:titleArea.bottom
                      anchors.topMargin:5
                      anchors.left:parent.left
                      anchors.right:parent.right
                      clip:true
                      height:325
                      tabPosition:Qt.BottomEdge
                      Component.onCompleted:{
                          var i = controller.getNumOfMarkerTypes();
                          for (var j = 0; j< i; j++) {
                              var tab = addTab(controller.getMarkerTypeString(j),waypointIconTabView)
                              tab.active = true;
                              console.log("\t\t>>>>>>Tab", tab.progress, tab.item)
                              tab.item.markerType = j;
                              //    tab.loaded.connect(wayPointTabView.xxx)
                              //      tab.loaded.connect(function() { tab.item.markerType = j; })
                              tab.item.focus = true
                          }
                          currentIndex = 1;
                      }
                  }
              
              Now controller is a c++ class.  It has the two methods defined as:
              (Controller.h)
              

              Q_INVOKABLE int getNumOfMarkerTypes();
              Q_INVOKABLE QString getMarkerTypeString(int);

              E 1 Reply Last reply
              0
              • D DBoosalis

                @dheerendra

                Do you really need a model in C++ to do this. Here is how I create a row of tabs dynamically in QML:

                 TabView {
                        id:wayPointTabView
                
                        anchors.top:titleArea.bottom
                        anchors.topMargin:5
                        anchors.left:parent.left
                        anchors.right:parent.right
                        clip:true
                        height:325
                        tabPosition:Qt.BottomEdge
                        Component.onCompleted:{
                            var i = controller.getNumOfMarkerTypes();
                            for (var j = 0; j< i; j++) {
                                var tab = addTab(controller.getMarkerTypeString(j),waypointIconTabView)
                                tab.active = true;
                                console.log("\t\t>>>>>>Tab", tab.progress, tab.item)
                                tab.item.markerType = j;
                                //    tab.loaded.connect(wayPointTabView.xxx)
                                //      tab.loaded.connect(function() { tab.item.markerType = j; })
                                tab.item.focus = true
                            }
                            currentIndex = 1;
                        }
                    }
                
                Now controller is a c++ class.  It has the two methods defined as:
                (Controller.h)
                

                Q_INVOKABLE int getNumOfMarkerTypes();
                Q_INVOKABLE QString getMarkerTypeString(int);

                E Offline
                E Offline
                Eeli K
                wrote on last edited by
                #7

                @DBoosalis That will not be updated automatically, which dheerendra explicitly wanted.

                1 Reply Last reply
                0

                • Login

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