Send QVariantList and QVariantMap to JavaScript Array and Object



  • Hi, I tried to send QVariantList and QVariantMap to JavaScript Array and Object, and did copy and paste example code in Data Type Conversion Between QML and C++ (http://qt-project.org/doc/qt-5/qtqml-cppintegration-data.html) but it didn't work, can anyone help me how i can do that in qt 5.3?



  • 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



  • 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])
        }
    }
    

    }@



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



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



  • 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)));
    ^


  • Moderators

    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 ?



  • No, it isn't inside the another Component


  • Moderators

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



  • Thanks for replying, but the error occurred again.


  • Moderators

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



  • 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])
        }
    }
    

    }@


  • Moderators

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



  • 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


  • Moderators

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



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


  • Moderators

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



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


  • Moderators

    Good... Happy Coding :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.