alternative for QJsonObject::fromVariantMap(data.value<QJSValue().toVariant().toMap())?



  • I am trying to send json data between QML and C++ trough signal and slot.I don't want to stringily json.

    Now I use this. is there a easier way ?

    //qml
    signal sendResponse(var data);

    //c++
    QObject::connect(this->objects[reference], SIGNAL(sendResponse(QVariant)), this, SLOT(parseResponse(QVariant)));

    void DisplayInterface::parseResponse(QVariant data)
    {
    qDebug() << QJsonObject::fromVariantMap(data.value<QJSValue().toVariant().toMap());
    }



  • Hi @Jens-van-koolwijk,

    qDebug() << QJsonObject::fromVariantMap(data.value<QJSValue().toVariant().toMap());

    I'm not familiar with QML, nor QJSValue, but just from the docs, I'd start with something like:

    qDebug() << data.type() << data.typeName();
    qDebug() << data.toJsonObject();
    qDebug() << data.toJsonObject().toVariantMap();
    

    What does that output?

    Cheers.



  • @Paul-Colby

    output:
    QVariant::QWidget* QJSValue
    QJsonObject()
    QMap()



  • @Jens-van-koolwijk said:

    QVariant::QWidget* QJSValue

    Ah, so you'll need to use QVariant::toJSValue first, eg:

    qDebug() << data.type() << data.typeName();
    qDebug() << data.toJsonValue().type() << data.toJsonValue();
    qDebug() << data.toJsonValue().toObject();
    qDebug() << data.toJsonValue().toObject().toVariantMap();
    

    See if that works.



  • @Paul-Colby

    output:
    QVariant::QWidget* QJSValue
    0 QJsonValue(null)
    QJsonObject()
    QMap()

    Hello Paul.

    I've read
    Qt 5.3 changed the mapping of "var" parameters in QML declared signals to QJSValue. This was reverted
    to the behavior of earlier Qt versions to use QVariant. The original issue
    of not being able to pass function objects through var parameters of QML
    declared signals is solved by wrapping a QJSValue inside the Variant.

    So i have to do this
    QJsonObject::fromVariantMap(data.value<QJSValue().toVariant().toMap());

    But to get json from QML to c++ this looks a little bit ugly to me.


Log in to reply
 

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