访问QJSEngine中的动态属性
Access dynamic property in QJSEngine
我可以访问传递到QJSEngine
的QObject
的属性,但为什么不能访问动态属性?
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;
退货:
4.2
35
0
使用Qt 5.2。
似乎这可能是QML中的一个错误。如果你使用QScriptEngine,问题似乎会消失,
#include <QScriptEngine>
#include <QCoreApplication>
#include <QDebug>
int main(int a, char *b[])
{
QCoreApplication app(a,b);
auto myObject = new QObject;
QScriptEngine engine;
auto scriptMyObject = engine.newQObject( myObject );
myObject->setProperty( "newProp", 35 );
engine.globalObject().setProperty( "myObject" , scriptMyObject );
qDebug() << myObject->property( "newProp" ).toInt();
qDebug() << engine.evaluate( "myObject.newProp" ).toInteger();
qDebug() << engine.evaluate( "myObject.newProp = 45" ).toInteger();
qDebug() << myObject->property( "newProp" ).toInt();
qDebug() << " -------- ";
// still can't create new properties from JS?
qDebug() << engine.evaluate( "myObject.fancyProp = 30" ).toInteger();
qDebug() << myObject->property("fancyProp").toInt();
return 0;
}
中的结果
35
35
45
45
--------
30
0
因此,这看起来像是QJSEngine中的一个错误,因为bahaviour与QScriptEngine不同。
相关文章:
- 如何在一个属性中动态存储基元类型
- 我可以从样式表 (qss) 更改自定义动态属性吗?
- 动态分配的属性和继承
- 如何根据道具名称动态设置对象的属性?
- 从高度动态的C 数据模型中更新QML:计时器与属性绑定
- C QT5动态属性
- 通过"a pointer of the base class"访问未在基类中声明的子类的方法或属性(动态)
- 从 QObject 中删除动态属性
- 私有属性C++上的动态二维数组
- C++ 类两个动态属性
- Qt:我什么时候可以从qtcreator访问动态属性
- 为@dynamic属性动态创建getter和setter,导致无限循环
- C++动态属性
- 在c++中处理类的动态属性
- 通过boost::read_graphviz()读取boost动态属性时发生异常
- 使用boost图库将boost动态属性写入文件
- 更改 Qt 动态属性的值时的调用方法
- 绑定和动态属性
- Qt:在给定的QObject上设置的动态属性列表
- 访问QJSEngine中的动态属性