I'm trying to learn about QT script and I'm clearly not understanding something basic.
Consider the following code:
#include <iostream>
#include <QApplication>
#include <QtScript>
#include <QObject>
using namespace std;
{
public:
int x;
};
int main(int argc, char* argv[])
{
MyClass mc;
mc.x = 0;
QScriptEngine eng;
QScriptValue scriptClass = eng.newQObject(&mc);
QScriptValue global = eng.globalObject();
global.setProperty("myClass", scriptClass);
QScriptValue res = eng.evaluate("myClass.x == 0");
if (res.isBoolean())
{
cout << "res is 0 = " << res.toBoolean() << endl;
}
else
{
cout << "res is not boolean!!!" << endl;
}
return 0;
}
#include <iostream>
#include <QApplication>
#include <QtScript>
#include <QObject>
using namespace std;
class MyClass : public QObject
{
public:
int x;
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyClass mc;
mc.x = 0;
QScriptEngine eng;
QScriptValue scriptClass = eng.newQObject(&mc);
QScriptValue global = eng.globalObject();
global.setProperty("myClass", scriptClass);
QScriptValue res = eng.evaluate("myClass.x == 0");
if (res.isBoolean())
{
cout << "res is 0 = " << res.toBoolean() << endl;
}
else
{
cout << "res is not boolean!!!" << endl;
}
return 0;
}
To copy to clipboard, switch view to plain text mode
This prints "res is 0 = 0" when run. It seems that the script engine does know about myClass but the value of myClass.x is not set.
Can someone enlighten me?
Thanks,
Steve
Bookmarks