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. Passing Array from C++ to QML?
Forum Updated to NodeBB v4.3 + New Features

Passing Array from C++ to QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 6 Posters 7.8k Views 3 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.
  • M Offline
    M Offline
    MAthias_Va
    wrote on 20 Feb 2019, 09:51 last edited by
    #1

    I need to pass Array of int, from C++ to QML and Vice Versa! I am confused which direction should I follow! any Advice?

    R B 2 Replies Last reply 20 Feb 2019, 10:02
    0
    • M MAthias_Va
      20 Feb 2019, 09:51

      I need to pass Array of int, from C++ to QML and Vice Versa! I am confused which direction should I follow! any Advice?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 20 Feb 2019, 10:02 last edited by
      #2

      @MAthias_Va
      you cannot pass an array directly to QML, but a QVariantList (see this)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • M MAthias_Va
        20 Feb 2019, 09:51

        I need to pass Array of int, from C++ to QML and Vice Versa! I am confused which direction should I follow! any Advice?

        B Offline
        B Offline
        Bhushan_Sure
        wrote on 20 Feb 2019, 10:11 last edited by
        #3

        @MAthias_Va you can access array element which is in C++ from qml, just call the function from qml and pass the parameter of "index". The function which is in C++, make that return type as "int or float" according to your requirement, by which you can get values in qml from C++ for that you don't need to pass array to qml

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MAthias_Va
          wrote on 20 Feb 2019, 10:22 last edited by
          #4

          Can anyone provide a simple Example? I couldn't understand the provided doc?

          O 1 Reply Last reply 20 Feb 2019, 10:49
          0
          • M MAthias_Va
            20 Feb 2019, 10:22

            Can anyone provide a simple Example? I couldn't understand the provided doc?

            O Offline
            O Offline
            ODБOï
            wrote on 20 Feb 2019, 10:49 last edited by
            #5

            hi
            @MAthias_Va

            have a class with Q_PROPERTY QVariantList

            ...
            Q_PROPERTY(QVariantList arr READ getarr WRITE setarr)
            public:
                const QVariantList & getarr() const { return m_arr; }
                void setarr(const QVariantList & v) { m_arr = v; }
            private : 
               QVariantList  m_arr;
            ...
            
            //fill the m_arr in ctor for example
            ...
             m_arr<<"1111"<<1111;
            ...
            

            set an instance of that class as contextProperty so it can be reached from QML

              ThatClass bk;
              engine.rootContext()->setContextProperty("backend",&bk);
            

            then use it

                property variant varList 
                Component.onCompleted : varList=backend.getarr
                onVarListChanged: {
            
                    for (var i in varList){
                        console.log(i)
                    }
                }
            
            M 1 Reply Last reply 20 Feb 2019, 14:22
            2
            • G Offline
              G Offline
              GrecKo
              Qt Champions 2018
              wrote on 20 Feb 2019, 10:59 last edited by GrecKo
              #6

              Exposing a QList<int> as a property kind of works.
              It's not directly usable as a model though, but since 5.12 you can easily make it work by doing : model: Array.from(myIntList)

              You can also modify it by doing myIntList[index] = 42 or myIntList.push(51) (look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array for reference)

              1 Reply Last reply
              3
              • O ODБOï
                20 Feb 2019, 10:49

                hi
                @MAthias_Va

                have a class with Q_PROPERTY QVariantList

                ...
                Q_PROPERTY(QVariantList arr READ getarr WRITE setarr)
                public:
                    const QVariantList & getarr() const { return m_arr; }
                    void setarr(const QVariantList & v) { m_arr = v; }
                private : 
                   QVariantList  m_arr;
                ...
                
                //fill the m_arr in ctor for example
                ...
                 m_arr<<"1111"<<1111;
                ...
                

                set an instance of that class as contextProperty so it can be reached from QML

                  ThatClass bk;
                  engine.rootContext()->setContextProperty("backend",&bk);
                

                then use it

                    property variant varList 
                    Component.onCompleted : varList=backend.getarr
                    onVarListChanged: {
                
                        for (var i in varList){
                            console.log(i)
                        }
                    }
                
                M Offline
                M Offline
                MAthias_Va
                wrote on 20 Feb 2019, 14:22 last edited by
                #7

                Thx @LeLev, this was really helpfull, Now I am enjoying editing my array in QML :)

                O 1 Reply Last reply 20 Feb 2019, 14:25
                0
                • M MAthias_Va
                  20 Feb 2019, 14:22

                  Thx @LeLev, this was really helpfull, Now I am enjoying editing my array in QML :)

                  O Offline
                  O Offline
                  ODБOï
                  wrote on 20 Feb 2019, 14:25 last edited by
                  #8

                  @MAthias_Va Nice happy coding !

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    joaogatao
                    wrote on 16 Nov 2020, 15:36 last edited by
                    #9

                    @LeLev Thx for the snippet of code! I was looking for something like this.
                    It works fine for passing the entire array from QML to C++, but not for a single element in the list.

                    In my application I have a matrix of on/off buttons, so essentially several lists of booleans. I wonder if there is a more lightweight solution than passing the entire QVariantList each time a boolean flips value.

                    O 1 Reply Last reply 16 Nov 2020, 15:48
                    0
                    • J joaogatao
                      16 Nov 2020, 15:36

                      @LeLev Thx for the snippet of code! I was looking for something like this.
                      It works fine for passing the entire array from QML to C++, but not for a single element in the list.

                      In my application I have a matrix of on/off buttons, so essentially several lists of booleans. I wonder if there is a more lightweight solution than passing the entire QVariantList each time a boolean flips value.

                      O Offline
                      O Offline
                      ODБOï
                      wrote on 16 Nov 2020, 15:48 last edited by
                      #10

                      @joaogatao hi,
                      you can have a Q_Invokable method that takes the index you want to return

                      Q_INVOKABLE bool getButtonIndex(int i){
                      //err check 
                       return arr.at(i);
                      }
                      

                      note if you bind this to a value in QML, it will not refresh automatically when you change values in the c++ side
                      //ex:

                      CheckBox{
                       checked : myObj.getButtonIndex(1) // will not be refreshed if **m_arr** changes c++ side
                      }
                      
                      1 Reply Last reply
                      1
                      • J Offline
                        J Offline
                        joaogatao
                        wrote on 16 Nov 2020, 16:53 last edited by
                        #11

                        @LeLev and thx for fast response on an old topic :)
                        I just tried it and it does what I need

                        I'm pretty new to Qt. So much to learn.

                        Cheers

                        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