zephod
19th April 2012, 03:47
I've have been investigating QtScript and I wrote a little test program which worked. The expression first evaluates false and the second time true.
#include <iostream>
#include <QApplication>
#include <QtScript>
#include <QObject>
#include <QWidget>
class MyClass : public QObject
{
Q_PROPERTY(bool x)
Q_PROPERTY(int y)
};
using namespace std;
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyClass mc;
QScriptEngine eng;
QScriptValue scriptClass = eng.newQObject(&mc);
QScriptValue global = eng.globalObject();
global.setProperty("myClass", scriptClass);
const QString expr = "myClass.x == true && myClass.y == 2";
mc.setProperty("x", true);
mc.setProperty("y", 0);
QScriptValue res = eng.evaluate(expr);
cout << "result = " << res.toBoolean() << endl;
mc.setProperty("y", 2);
res = eng.evaluate(expr);
cout << "result = " << res.toBoolean() << endl;
return 0;
}
Then it occurred to me that perhaps it should not have compiled. The documentation for Q_PROPERTY says that
The property name and type and the READ function are required but I didn't have a read function defined so how did that work?
The next thing I wanted to do was to add a slot so I added Q_OBJECT above the Q_PROPERTY line, ran gmake clean, make-qt4 and gmake. I got these linker errors:
main.o: In function `MyClass': undefined reference to `vtable for MyClass'
main.o: In function `~MyClass': undefined reference to `vtable for MyClass'
It's referring to a constructor and destructor I don't have so I tried adding my own empty ones but that didn't help.
Next I though that perhaps I have to define at least one slot or signal so I added one but that didn't help.
Here is where it gets a little weird, at least to me. I took the class definition and put it in a .h file without changing anything apart from adding the #include of course, re-ran everything and now it links OK and I get warnings about the Q_PROPERTY macros not having a read function and that the properties will be invalid. So I added the read functions to the Q_PROPERTY lines, defined the functions and added some private variables.
Cool! now everyting compiles and links without errors and warnings.
Just one problem - the evaluation of the expression is now false in both cases!!!
#include <iostream>
#include <QApplication>
#include <QtScript>
#include <QObject>
#include <QWidget>
class MyClass : public QObject
{
Q_PROPERTY(bool x)
Q_PROPERTY(int y)
};
using namespace std;
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MyClass mc;
QScriptEngine eng;
QScriptValue scriptClass = eng.newQObject(&mc);
QScriptValue global = eng.globalObject();
global.setProperty("myClass", scriptClass);
const QString expr = "myClass.x == true && myClass.y == 2";
mc.setProperty("x", true);
mc.setProperty("y", 0);
QScriptValue res = eng.evaluate(expr);
cout << "result = " << res.toBoolean() << endl;
mc.setProperty("y", 2);
res = eng.evaluate(expr);
cout << "result = " << res.toBoolean() << endl;
return 0;
}
Then it occurred to me that perhaps it should not have compiled. The documentation for Q_PROPERTY says that
The property name and type and the READ function are required but I didn't have a read function defined so how did that work?
The next thing I wanted to do was to add a slot so I added Q_OBJECT above the Q_PROPERTY line, ran gmake clean, make-qt4 and gmake. I got these linker errors:
main.o: In function `MyClass': undefined reference to `vtable for MyClass'
main.o: In function `~MyClass': undefined reference to `vtable for MyClass'
It's referring to a constructor and destructor I don't have so I tried adding my own empty ones but that didn't help.
Next I though that perhaps I have to define at least one slot or signal so I added one but that didn't help.
Here is where it gets a little weird, at least to me. I took the class definition and put it in a .h file without changing anything apart from adding the #include of course, re-ran everything and now it links OK and I get warnings about the Q_PROPERTY macros not having a read function and that the properties will be invalid. So I added the read functions to the Q_PROPERTY lines, defined the functions and added some private variables.
Cool! now everyting compiles and links without errors and warnings.
Just one problem - the evaluation of the expression is now false in both cases!!!