Important: Please read the Qt Code of Conduct -

JSON string to QML, "null" values converted to QVariant(void*) with QJSEngine::toScriptValue

  • hello,

    I don't know if this is bug but i'm trying to convert a JSON string to a QJSValue to use it directly in QML.
    it works great so far but I have some weird results with "null" values in the JSON data.

    minimal example setup:
    simple JSON object as string: { "test": null }

    so I convert that to a JSValue like this:
    QJSValue data = QJSEngine().toScriptValue(QJsonDocument::fromJson("{ "test": null }").object());
    I didn't found a doc but it seems to work to convert a QJsonObject to QJSValue.

    my problem now is that the "test" property will be QVariant(void*) and not simply "null" as expected in JavaScript.

    First converting the QJsonDocument to QVariant and than to QJSValue works (the value is then QVariant(Invalid) which gets converted to "null" in JS) but I think that is an unnecessary step?
    also I get a segmantation fault for some QVariantMaps, see my call stack here:

    0 QV4::PropertyHash::lookup qv4internalclass.cpp 118 0x826b8ed
    1 QV4::InternalClass::addMember qv4internalclass.cpp 211 0x826bd5d
    2 QV4::Object::insertMember qv4object.cpp 297 0x828c504
    3 QV8Engine::variantMapToJS qv8engine.cpp 595 0x835984c
    4 QV8Engine::metaTypeToJS qv8engine.cpp 688 0x8359e48
    5 QV8Engine::variantToJS qv8engine.cpp 918 0x835aef1
    6 QV8Engine::variantMapToJS qv8engine.cpp 596 0x8359869
    7 QV8Engine::metaTypeToJS qv8engine.cpp 688 0x8359e48
    8 QV8Engine::variantToJS qv8engine.cpp 918 0x835aef1
    9 QV8Engine::metaTypeToJS qv8engine.cpp 698 0x8359f5f
    10 QJSEngine::create qjsengine.cpp 363 0x8252b85
    11 QJSEngine::toScriptValue<QVariant> qjsengine.h 82 0x40e7b3
    ... <More>

    any ideas?? :)

    one last thing, in general is it better to convert a QJsonDocument to QJSValue or just use an QVariant and pass that to QML (via Qt signal argument)?

    Edit: I use Qt 5.2 mingw on windows, but I don't think that matters much here

  • I'm having the same issue: code from Qt 4 did not work because of that change.
    This bug should be fixed though with Qt 5.3 RC1 (ETA next week):

    Cheers, Chris

Log in to reply