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 8.0k 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 MAthias_Va

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

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on 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

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

      Bhushan_SureB Offline
      Bhushan_SureB Offline
      Bhushan_Sure
      wrote on 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 last edited by
        #4

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

        ODБOïO 1 Reply Last reply
        0
        • M MAthias_Va

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

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on 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
          2
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on 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
            • ODБOïO ODБOï

              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 last edited by
              #7

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

              ODБOïO 1 Reply Last reply
              0
              • M MAthias_Va

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

                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by
                #8

                @MAthias_Va Nice happy coding !

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  joaogatao
                  wrote on 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.

                  ODБOïO 1 Reply Last reply
                  0
                  • J joaogatao

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

                    ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on 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 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