Reputation: 720
Hi there I want pass javascript function to c++ code from qml and later call it, but stuck with QScriptValue holding QVariantMap, which dont want to be accessed from javascript. Am I doing everything in right way and can I do what I want without hacks? Im using QtQuick1.1 and Qt 5.5.0.
I have c++ class with method:
void SomeClass::setQmlCallback(const QScriptValue &fn)
{
QScriptValue scriptFn = fn;
QScriptValueList args;
QVariantMap m;
m.insert(QStringLiteral("a"), 96);
m.insert(QStringLiteral("b"), 97);
args << scriptFn.engine()->newVariant(m); // To pass VariantMap to qml I wrap it in ScriptValue
scriptFn.call(scriptFn, args);
}
And some qml code:
onSomethingHappend: {
SomeClass.setQmlCallback(function(row) {
// console.log(row.a); // NotOk
// row's key `a` is undefined here
var convertedToVariant = SomeClass.convertToVariant(row);
console.log(convertedToVariant.a); // Ok
});
}
Hack to explicitly convert ScriptValue to Variant:
QVariant SomeClass::convertToVariant(const QVariant &v)
{
return v;
}
Also if I will pass QString instead of QVariantMap everything will work
Upvotes: 0
Views: 340
Reputation: 720
I dont know why, but changing QScriptEngine::newVariant
to QScriptEngine::toScriptValue
solves the issue so I can access passed QVariantMap as js object without hacks.
Upvotes: 1