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 QVariantList and QVariantMap to JavaScript Array and Object
Servers for Qt installer are currently down

Send QVariantList and QVariantMap to JavaScript Array and Object

Scheduled Pinned Locked Moved QML and Qt Quick
19 Posts 3 Posters 10.5k Views 1 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.
  • L Offline
    L Offline
    leonardo.ramosantos
    wrote on last edited by
    #2

    Hi,

    There is a error message? If you describe more your problem, it can be more easy to solve.

    However you can check if you are doing on this way:

    @QMetaObject::invokeMethod(<qml_object>, "<method_to_receive_your_map>", Qt::BlockingQueuedConnection, Q_ARG(QVariant, QVariant::fromValue(<your_map_variant>)));@

    []'s

    1 Reply Last reply
    0
    • B Offline
      B Offline
      beh_zad
      wrote on last edited by
      #3

      thanks for replying, as I said I did copy and paste that codes (I put my codes in below) and get this error "error: no matching function for call to 'QMetaObject::invokeMethod(QQuickItem*, const char [11], QArgument<QVariant>, QArgument<QVariant>)'
      Q_ARG(QVariant, QVariant::fromValue(map)));
      I did search to resolve it but i can't. my code
      in main.cpp
      @int main(int argc, char *argv[])
      {
      QGuiApplication app(argc, argv);

      QQuickView view(QUrl::fromLocalFile&#40;"main.qml"&#41;&#41;;
      
      QVariantList list;
      list << 10 << QColor(Qt::green) << "bottles";
      
      QVariantMap map;
      map.insert("language", "QML");
      map.insert("released", QDate(2010, 9, 21));
      
      QMetaObject::invokeMethod(view.rootObject(), "readValues",
              Q_ARG(QVariant, QVariant::fromValue(list)),
              Q_ARG(QVariant, QVariant::fromValue(map)));
      
      return app.exec&#40;&#41;;
      

      }
      @
      in main.qml

      @Item {
      function readValues(anArray, anObject) {
      for (var i=0; i<anArray.length; i++)
      console.log("Array item:", anArray[i])

          for (var prop in anObject) {
              console.log("Object item:", prop, "=", anObject[prop])
          }
      }
      

      }@

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leonardo.ramosantos
        wrote on last edited by
        #4

        Maybe the problem is your view's variable type.

        Here, I've tested the same code as you and it worked. However, I've changed the view type to "QtQuick1ApplicationViewer" and I've used "setMainQmlFile" to set my Qml file's path.

        Try to change this and post your results.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          beh_zad
          wrote on last edited by
          #5

          I have not worked ever with this code, and when i did search I couldn't find good things, is there any way to do that with previous view type?

          1 Reply Last reply
          0
          • B Offline
            B Offline
            beh_zad
            wrote on last edited by
            #6

            thank for replying me again, I have done with this view type and get this error again.
            bq. error: no matching function for call to 'QMetaObject::invokeMethod(QGraphicsObject*, const char [11], QArgument<QVariant>, QArgument<QVariant>)'
            Q_ARG(QVariant, QVariant::fromValue(map)));
            ^

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #7

              Hi,

              bq. “error: no matching function for call to ‘QMetaObject::invokeMethod(QQuickItem*, const char [11], QArgument<QVariant>, QArgument<QVariant>)’ Q_ARG(QVariant, QVariant::fromValue(map)));

              @
              Item {
              function readValues(anArray, anObject) {
              for (var i=0; i<anArray.length; i++)
              console.log("Array item:", anArray[i])
              for (var prop in anObject) {
              console.log("Object item:", prop, "=", anObject[prop])
              }
              }
              }
              @

              Does your Item element have a parent ? In other words, is the Item element inside another Component ?

              157

              1 Reply Last reply
              0
              • B Offline
                B Offline
                beh_zad
                wrote on last edited by
                #8

                No, it isn't inside the another Component

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #9

                  Ok. Then include this in main.cpp
                  @
                  #include <QtQuick>
                  @

                  157

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    beh_zad
                    wrote on last edited by
                    #10

                    Thanks for replying, but the error occurred again.

                    1 Reply Last reply
                    0
                    • p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #11

                      Are you still using QQuickView ? Why is there QGraphicsObject in error ?

                      bq. QMetaObject::invokeMethod(QGraphicsObject*, const char [11], QArgument<QVariant>, QArgument<QVariant>)’ Q_ARG(QVariant, QVariant::fromValue(map)));

                      157

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        beh_zad
                        wrote on last edited by
                        #12

                        the whole codes is :

                        in main.cpp

                        #include <QtCore>
                        #include <QtDeclarative>

                        @int main(int argc, char *argv[])
                        {
                        QGuiApplication app(argc, argv);

                        QDeclarativeView view(QUrl::fromLocalFile&#40;"qrc:///main.qml"&#41;);
                        
                        QVariantList list;
                        list << 10 << QColor(Qt::green) << "bottles";
                        
                        QVariantMap map;
                        map.insert("language", "QML");
                        map.insert("released", QDate(2010, 9, 21));
                        
                        QMetaObject::invokeMethod( view.rootObject(), "readValues",
                                                  Q_ARG(QVariant, QVariant::fromValue(list)),
                                                  Q_ARG(QVariant, QVariant::fromValue(map)));
                        
                        view.setSource(QUrl::fromLocalFile&#40;"qrc:///main.qml"&#41;);
                        view.show();
                        
                        return app.exec();
                        

                        }@

                        in main.qml

                        @import QtQuick 2.2
                        import QtQuick.Window 2.1

                        Item {
                        function readValues(anArray, anObject) {
                        for (var i=0; i<anArray.length; i++)
                        console.log("Array item:", anArray[i])

                            for (var prop in anObject) {
                                console.log("Object item:", prop, "=", anObject[prop])
                            }
                        }
                        

                        }@

                        1 Reply Last reply
                        0
                        • p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #13

                          @
                          QDeclarativeView view(QUrl::fromLocalFile("qrc:///main.qml"));
                          @

                          Don't use QDeclarativeView; use QQuickView instead.

                          Your earlier code was correct:
                          @
                          #include <QGuiApplication>
                          #include <QtQuick>

                          int main(int argc, char *argv[])
                          {
                          QGuiApplication app(argc, argv);

                          QQuickView view(QUrl::fromLocalFile&#40;"main.qml"&#41;);
                           
                          QVariantList list;
                          list << 10 << QColor(Qt::green) << "bottles";
                           
                          QVariantMap map;
                          map.insert("language", "QML");
                          map.insert("released", QDate(2010, 9, 21));
                           
                          QMetaObject::invokeMethod(view.rootObject(), "readValues",
                                  Q_ARG(QVariant, QVariant::fromValue(list)),
                                  Q_ARG(QVariant, QVariant::fromValue(map)));
                           
                          view.show();
                          return app.exec();
                          

                          }
                          @

                          Try with this code again and check.

                          157

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            beh_zad
                            wrote on last edited by
                            #14

                            but this code show this error:
                            error: no matching function for call to 'QMetaObject::invokeMethod(QQuickItem*, const char [11], QArgument<QVariant>, QArgument<QVariant>)'
                            Q_ARG(QVariant, QVariant::fromValue(map)));

                            I tried it again but this error is displayed

                            1 Reply Last reply
                            0
                            • p3c0P Offline
                              p3c0P Offline
                              p3c0
                              Moderators
                              wrote on last edited by
                              #15

                              Strange.. It works perfectly for me. Which Qt version and OS are you using ?

                              157

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                beh_zad
                                wrote on last edited by
                                #16

                                It's storage for me too, Qt Creater 3.1.1 based on Qt 5.2.1

                                1 Reply Last reply
                                0
                                • p3c0P Offline
                                  p3c0P Offline
                                  p3c0
                                  Moderators
                                  wrote on last edited by
                                  #17

                                  Can you RightClick on QMetaObject > Refactor > Add #Includes ... (any or all which it mentions) ?

                                  157

                                  1 Reply Last reply
                                  0
                                  • B Offline
                                    B Offline
                                    beh_zad
                                    wrote on last edited by
                                    #18

                                    I had done it at least, it needs to include <QQuickItem>, thanks for everybody to replying my question.

                                    1 Reply Last reply
                                    0
                                    • p3c0P Offline
                                      p3c0P Offline
                                      p3c0
                                      Moderators
                                      wrote on last edited by
                                      #19

                                      Good... Happy Coding :)

                                      157

                                      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