2014-04-07 6 views
1

QObject의 속성에 액세스 할 수 있지만 QJSEngine에 전달되었지만 동적 속성에 액세스 할 수없는 이유는 무엇입니까? Qt는 5.2을 사용하여QJSEngine의 동적 속성에 액세스

4.2 
35 
0 

:

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; 

돌아갑니다.

답변

1

QML에서 버그 일 수 있습니다. 대신 QScriptEngine를 사용하는 경우, 문제는 사라 따라서

35 
35 
45 
45 
-------- 
30 
0 

bahaviour이 QScriptEngine 다르듯이 QJSEngine의 버그처럼 보이는에서

#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; 
} 

결과를 보인다.

+0

당신 말이 맞아요, 그것은 회귀 것으로 보입니다. 여기에 버그를 열었습니다 : https://bugreports.qt-project.org/browse/QTBUG-38181 – cmannett85

+0

이것은 버그가 아니며 Qt는 QJSEngine에서 이것을 지원하지 않습니다 : http://doc.qt.io /qt-5/qjsengine.html (동적 QObject 속성 장 검색) – jaba