How send me data json from QML in C++?
-
I tried make this so:
@
//main.qml
Rectangle {
//---
signal sgSendJSON(var v)
function sendJSON() {
var v = {title: "text", text: "text"}
sgSendJSON(v)
}
//---
}//main.cpp
//---
pView = new QQuickView;
QObject *pObject = pView->rootObject();connect(pObject, SIGNAL(sgSendJSON(QVariant)), this, SLOT(SendJSON(QVariant)));
//---
@But I failed(
-
Hi,
Please describe what you mean by "failed". Did your application fail to compile? Did it compile but the rectangle failed to show up? Or ...?
Anyway, see "Connecting C++ Objects to QML Objects":http://doc.qt.io/qt-5/signalsandslots-syntaxes.html#connecting-c-objects-to-qml-objects
-
Hi,
@
var v = {title: "text", text: "text"}
@I think this is not a JSON format as the key should be a string format i.e in "".
Its more like a map which you can convert to on C++ side
@
void SendJSON(QVariant v) {
qDebug() << v.toMap();
}
@ -
You can get the JavaScript object's property values like that:
@
void SendJSON(QVariant v) {
QString title = v.toMap().value("title").toString();
QString text = v.toMap().value("text").toString();
}
@