As qt docs on QJSValue
, QJsonObject
ins't implicitly convertible to QJSValue
, I want to call a javascript function with QJSEngine
from C++, the arguments should be passed with QList<QJsValue>
to call function of another QJSValue
which holds the function itself.
The problem is one of my arguments is QJsonObject
, until now i am supposed to convert it to text then calling and passing it to the javascript function that calls to JSON.parse
for converting it to object, i am looking for a solution that lets me convert QJsonObject
into QJSValue
in C++ and call javascript function with object arguments instead of json text.
Currently the code is like something below
QJsonObject obj;
obj["1"] = QString("A");
obj["2"] = QString("B");
QJSValue func = myEngine.evaluate("(function(arg) { var obj = JSON.parse(arg); var res = obj[\"1\"] + obj[\"2\"]; return res; })");
QJSValueList args;
args << QString(QJsonDocument(obj).toJson());
QJSValue res = func.call(args);
I would like to have a function like QJSValue ConvertToQJSValue(QJsonObject object)
something like this :
QJsonObject obj;
obj["1"] = QString("A");
obj["2"] = QString("B");
QJSValue func = myEngine.evaluate("(function(arg) { var res = arg[\"1\"] + arg[\"2\"]; return res; })");
QJSValueList args;
args << ConvertToQJSValue(obj);
QJSValue res = func.call(args);
As simple as QJSEngine::toScriptValue(const T &).
T
must be aQJsonObject
orQJsonArray
, not aQJsonDocument
.