QJsonDocument as parameter in emit : when i read the json in javascpit it returns null
-
HI,
I have a project with qtwebengine where I call a method that prepares a json in QJsonDocument format and then puts it as a parameter in an emit signal.
When I read the json in the function that receives the data in javascript the json is empty.
where am i wrong?
Can i create a json and receive a json in the html/javascript page without going through the conversion to string?Thanks in advance
Code with return null or {} with parameter type QJsonObject
json.setArray(recordsArray); QJsonObject json_obj = json.object(); emit testJsonReturn(json_obj);OR
Code with return null or {} with parameter type QJsonDocumentjson.setArray(recordsArray); emit testJsonReturn(json);If I convert the same data into string and pass it to javascript the data is present.
QJsonDocument as parameter in emit : when i read the json in javascpit it returns null
Code correct:json.setArray(recordsArray); strJsonDati = json.toJson(QJsonDocument::Compact); emit testJsonReturn(strJsonDati); -
HI,
I have a project with qtwebengine where I call a method that prepares a json in QJsonDocument format and then puts it as a parameter in an emit signal.
When I read the json in the function that receives the data in javascript the json is empty.
where am i wrong?
Can i create a json and receive a json in the html/javascript page without going through the conversion to string?Thanks in advance
Code with return null or {} with parameter type QJsonObject
json.setArray(recordsArray); QJsonObject json_obj = json.object(); emit testJsonReturn(json_obj);OR
Code with return null or {} with parameter type QJsonDocumentjson.setArray(recordsArray); emit testJsonReturn(json);If I convert the same data into string and pass it to javascript the data is present.
QJsonDocument as parameter in emit : when i read the json in javascpit it returns null
Code correct:json.setArray(recordsArray); strJsonDati = json.toJson(QJsonDocument::Compact); emit testJsonReturn(strJsonDati);@elicat
You don't show how you try to pass and receive theQJson...objects from C++ to JavaScript in the QtWebEngine page. By the way, the fact your code usesemitis not relevant.But whatever. The C++
QJson...classes do not have the same representation as the JavaScriptJSONobjects. You cannot swap between these. So converting to string at sender side and re-parsing from string at receiver side seems reasonable/the only approach.EDIT
Hmm, perhaps I spoke too soon. I had a look at QJSEngine Class --- is that what you use withQtWebEngine? [I thought it was QML, dunno.] If it is that has template <typename T> QJSValue QJSEngine::toScriptValue(const T &value). https://stackoverflow.com/a/75154459/489865 claims you can pass aQJsonObjectorQJsonArraybut not aQJsonDocument.I am not understanding what you have in code which connects your
testJsonReturn()C++ signal to something in JavaScript on aQWebEnginepage? -
@elicat
You don't show how you try to pass and receive theQJson...objects from C++ to JavaScript in the QtWebEngine page. By the way, the fact your code usesemitis not relevant.But whatever. The C++
QJson...classes do not have the same representation as the JavaScriptJSONobjects. You cannot swap between these. So converting to string at sender side and re-parsing from string at receiver side seems reasonable/the only approach.EDIT
Hmm, perhaps I spoke too soon. I had a look at QJSEngine Class --- is that what you use withQtWebEngine? [I thought it was QML, dunno.] If it is that has template <typename T> QJSValue QJSEngine::toScriptValue(const T &value). https://stackoverflow.com/a/75154459/489865 claims you can pass aQJsonObjectorQJsonArraybut not aQJsonDocument.I am not understanding what you have in code which connects your
testJsonReturn()C++ signal to something in JavaScript on aQWebEnginepage?@JonB hello,
yes the connect is from qtwebengine and I have try also with QJsonObject (see previous example .Tihis is code for connect webengine
window.onload = function () { new QWebChannel(qt.webChannelTransport, function (channel) { webobj = channel.objects.wiseMan; window.foo = webobj; // connect signal whith js code webobj.testJsonReturn.connect(testJsonReturn); var jsonData = {} jsonData.xxx = "xx" // example jsonData.boxWidth = withContentBox.toString(); foo.sqlToJson(jsonData); // call C++ }); }This is example js code
function testJsonReturn(JsonDataReturn) { console.log(JSON.stringify(JsonDataReturn)); }and I have result
{} -
@JonB hello,
yes the connect is from qtwebengine and I have try also with QJsonObject (see previous example .Tihis is code for connect webengine
window.onload = function () { new QWebChannel(qt.webChannelTransport, function (channel) { webobj = channel.objects.wiseMan; window.foo = webobj; // connect signal whith js code webobj.testJsonReturn.connect(testJsonReturn); var jsonData = {} jsonData.xxx = "xx" // example jsonData.boxWidth = withContentBox.toString(); foo.sqlToJson(jsonData); // call C++ }); }This is example js code
function testJsonReturn(JsonDataReturn) { console.log(JSON.stringify(JsonDataReturn)); }and I have result
{}@elicat
You did not mention that you are usingQWebChannel, do you not think that was relevant?I have never looked at it. I believe it allows the connection of signals from the C++ side to slots on the JS side.
However, you are passing a C++/Qt
QJson...object as a parameter from C++ to JS. And then you are trying to use the JSJSONclass/calls on that. Unless you/someone says they are in any way compatible, my guess is that these two internal representation of JSON objects have nothing in common? In which case you won't be able to have them interact like this. At best you might use thewebobjobject to call C++-sideQJson...methods on the JSON objects instead of the JS-sideJSONcalls.Your question was asked in How send a QJsonObject using QWebChannel in Qt. The answer there was
Instead of using the
QJsonfamily objects, you can sendQVariantobjects to your Javascript code -
@elicat
You did not mention that you are usingQWebChannel, do you not think that was relevant?I have never looked at it. I believe it allows the connection of signals from the C++ side to slots on the JS side.
However, you are passing a C++/Qt
QJson...object as a parameter from C++ to JS. And then you are trying to use the JSJSONclass/calls on that. Unless you/someone says they are in any way compatible, my guess is that these two internal representation of JSON objects have nothing in common? In which case you won't be able to have them interact like this. At best you might use thewebobjobject to call C++-sideQJson...methods on the JSON objects instead of the JS-sideJSONcalls.Your question was asked in How send a QJsonObject using QWebChannel in Qt. The answer there was
Instead of using the
QJsonfamily objects, you can sendQVariantobjects to your Javascript code@JonB Hello thanks for your precision.
Is there another way to make a c++ method communicate with a javascript code of an html page loaded in a qml file? If there is I didn't know about it.
I have inserted the post in the QtWebEngine section specially.
Anyway, It is true that questions asked in the right way are easier to interpret and solve.After seeing the proposed link I solved it as follows.
QJsonDocument json(recordsArray); json.setArray(recordsArray); QJSEngine engine; QJSValue resDatiSqlJsonValue; if (json.isObject()) { resDatiSqlJsonValue = engine.toScriptValue(json.object()); } else if (json.isArray()) { resDatiSqlJsonValue = engine.toScriptValue(json.array()); } else { resDatiSqlJsonValue = engine.newErrorObject( QJSValue::TypeError, "JSON was neither an object nor array. Strange, right?" ); } emit testJsonReturn(resDatiSqlJsonValue);Thanks
-
E elicat has marked this topic as solved on