1
votes

I'm using Qt5.5 (5.5.1-3 from archlinux x86_64 repos) and want to read qml object property from cpp, no luck yet:

qml part:

import QtQuick 2.2

Item {
    signal doSomething()
    signal myDataChanged()
    property string myString: ""
    property var myObject: ({})
    onDoSomething(): {
        myString = "myStringValue"
        myObject = {"foo":"bar"}    
        myDataChanged()
    }
}

cpp part:

void MyClass::processChangedData() {
    qDebug()<<sender()->property("myString");
    qDebug()<<sender()->property("myObject");
}

myDataChanged() signal is connected to MyClass::processChangedData()

output:

QVariant(QString, "myString")
QVariant(QJSValue, )

Can you help me? I want to get the data of the qml object property from cpp part.

1
Since the answer is correct, given tour comment below, please accept It. Thanks. - BaCaRoZzo

1 Answers

2
votes

Sounds like you should be able to do:

auto js = sender()->property("myObject").value<QJSValue>();
qDebug() << js.property("foo").toString();

and use other methods in QJSValue.