Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Json document in a qml object



  • Hello,

    I am new to Qt Quick, so please excuse my beginner question (I come from C++)...

    I am trying to create a non visible component (QtObject) which will contain a number of information that my application needs. I am trying to do as much as I can in QML and not "drop back" to C++

    This data is saved as a Json file and I wanted to create a wrapper around a JsonDocument to access the data.
    Something around the lines of:
    However, QML does not seem to like the JsonDocument "type". What would be the proper way to achieve what I want?

    import QtQuick 2.0
    QtObject {
    property string jsonString: "{"a":"A whatever, run","b":"B fore something happens"}" // temporary to test
    JsonDocument { is: src; source: String }
    property string str1: src.a
    property string str2: src.b
    }

    Thanks!



  • Cant help you with Json but this may interest you https://wiki.qt.io/Qml_Styling



  • Hello,

    Cant help you with Json but this may interest you https://wiki.qt.io/Qml_Styling

    Thanks, this article on Singleton was actually useful!

    Cyrille



  • Hi Cyrille,
    the JavaScript Host Environment in QML provides the standard ECMAScript built-ins (http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html).

    There´s a JSON type you can use in javascript expression like
    var jsonObj = JSON.parse(yourJsonString)
    After parsing you can access the json objects for your example like jsonObj.a
    If you really need a QObject wrapper, you could parse the json at Component.onCompleted event
    and provide javascript functions to access your json objects.


Log in to reply