I can access properties of QObject
s passed into a QJSEngine
, but why can't I access dynamic properties?
auto myObject = new MyObject(); // Contains a single property 'myProp'.
QJSEngine engine;
auto scriptMyObject = engine.newQObject( myObject );
engine.globalObject().setProperty( "myObject" , scriptMyObject );
engine.evaluate( "myObject.myProp = 4.2" );
cout << engine.evaluate( "myObject.myProp" ).toNumber() << endl;
myObject->setProperty( "newProp", 35 );
cout << myObject->property( "newProp" ).toInt() << endl;
cout << engine.evaluate( "myObject.newProp" ).toInt() << endl;
Returns:
4.2
35
0
Using Qt 5.2.
Seems it may be a bug in QML. If you use QScriptEngine instead, the problem seems to go away,
results in
Therefore this looks like a bug in QJSEngine as the bahaviour differs from QScriptEngine.