qml与c++传递json数据

两边的json数据不能直接转换,要转成字符串才能传递:

c++传递给qml的数据:

QString getPageInfo()
{
    QJsonObject object;
    object["range"] = range;
    QJsonDocument document;
    document.setObject(object);

    return QString(document.toJson(QJsonDocument::Compact));
}

qml读取:

    var jobjectStr = rangerdiv.getPageInfo()
    var object = JSON.parse(jobjectStr)
    var value = object.range 

qml传递给c++的数据:

    let object = {}
    object.range = ranger_input.intext
    var objectStr = JSON.stringify(object)

c++解析:

    QJsonDocument doc = QJsonDocument::fromJson(jsonObjectString.toUtf8());
    QJsonObject object = doc.object();

你可能感兴趣的:(#,QML日常记录,qml)