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. Send array from QML to c++ via signal
Forum Updated to NodeBB v4.3 + New Features

Send array from QML to c++ via signal

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 2.6k 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I want to pass an array from QML to C++ via a signal:

    signal passArray(variant array)
    function emitPassArray() {
        var i
        var array = []
        for(i=0; i<gridView.model.count;i++) {
            var myob =gridView.model.items.get(i).model
            array.push(myob.property1)
        }
        passArray(array)
    }
    

    and I thought I catch it on the c++ side with a

        void getArray(QVariantList array);
    

    slot. However, I get the message, that it is not possible to connect getArray as there is no signal passArray(QVariantList). What am I doing wrong?

    raven-worxR 1 Reply Last reply
    0
    • M Offline
      M Offline
      maxwell31
      wrote on last edited by
      #2

      Calling the c++ slot in QML directly is working. Still, I would be interested in how it should be done if one were to use a signal

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

        You can expose qlist as qproperty from c++ and set the list in qml. No need pass signal and catch it. Is there a hard requirement to send signal only ?

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

        1 Reply Last reply
        0
        • M maxwell31

          Hi,

          I want to pass an array from QML to C++ via a signal:

          signal passArray(variant array)
          function emitPassArray() {
              var i
              var array = []
              for(i=0; i<gridView.model.count;i++) {
                  var myob =gridView.model.items.get(i).model
                  array.push(myob.property1)
              }
              passArray(array)
          }
          

          and I thought I catch it on the c++ side with a

              void getArray(QVariantList array);
          

          slot. However, I get the message, that it is not possible to connect getArray as there is no signal passArray(QVariantList). What am I doing wrong?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @maxwell31 said in Send array from QML to c++ via signal:

          However, I get the message, that it is not possible to connect getArray as there is no signal passArray(QVariantList). What am I doing wrong?

          check the output of the following:

          const QMetaObject* mo = myQmlObject->metaObject();
          for (int i = mo->methodOffset(); i < mo->propertyCount(); ++i) {
                  QMetaMethod m = mo->method(i);
                  if( m.methodType() != QMetaMethod::Signal )
                       continue;
                  qDebug() << m.methodSignature();
          }
          

          This prints the signatures of all the object's signals.

          I guess the signature is just wrong. Since you defined the signal in QML with a variant type, i think the correct parameter type will be QVariant instead.

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

          JKSHJ 1 Reply Last reply
          1
          • raven-worxR raven-worx

            @maxwell31 said in Send array from QML to c++ via signal:

            However, I get the message, that it is not possible to connect getArray as there is no signal passArray(QVariantList). What am I doing wrong?

            check the output of the following:

            const QMetaObject* mo = myQmlObject->metaObject();
            for (int i = mo->methodOffset(); i < mo->propertyCount(); ++i) {
                    QMetaMethod m = mo->method(i);
                    if( m.methodType() != QMetaMethod::Signal )
                         continue;
                    qDebug() << m.methodSignature();
            }
            

            This prints the signatures of all the object's signals.

            I guess the signature is just wrong. Since you defined the signal in QML with a variant type, i think the correct parameter type will be QVariant instead.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @raven-worx said in Send array from QML to c++ via signal:

            I guess the signature is just wrong. Since you defined the signal in QML with a variant type, i think the correct parameter type will be QVariant instead.

            Indeed. After that, you can convert the data using QVariant::toList().

            http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals says "When a QML object type is used as a signal parameter, the parameter should use var as the type, and the value should be received in C++ using the QVariant type"

            Also, according to http://doc.qt.io/qt-5/qml-variant.html, variant is "obsolete and exists only to support old applications; new applications should use var type properties instead."

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            3
            • M Offline
              M Offline
              maxwell31
              wrote on last edited by
              #6

              Yes, the problem was that I used QVariantList on the C++ side instead of QVariant. Thank you all for your help!

              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