PDA

View Full Version : QtScript



QTInfinity
20th November 2008, 11:18
Hi guys...

Currently i am learning QtScripts...and while trying out code snippets from Assistant.
I got a few errors....It is a programmatically incorrect. Could some body help me out with this code.

I am sending it as an attachment (Main.cpp).

In assistant section "Making C++ Class Member Functions Available in QtScript", QtScript overview

Consider this part

QScriptEngine eng;
eng.globalObject().setProperty("obj", eng.newQObject(myObj)); -------------(A)

qDebug() << eng.evaluate(
"var obj = new MyObject;\n" -------------(B)
- - -
"print('Enabled: ' + obj.enabled);\n"
"print('isEnabled: ' + obj.isEnabled());" -------------(C)
);

(A) As usual, MyObject needs to be available to the engine 'eng'.
(B) Gives an error stating that MyObject is not defined. I even tried removing this statement, since there is no use of redeclaring global property name 'obj' already declared in (A).
(C) Gives an error isEnabled() is not defined.
(D) Why is the inclusion of Q_OBJECT macro giving meta object errors? The attached code does not have one and is even executing.

How could i improvise on this??

QTInfinity
20th November 2008, 21:10
this one now works....



//CLASS DECL
//USE THE Q_OBJECT macro

#include "main.moc"

int main(int argc, char **argv)
{
Q_UNUSED(argc)
Q_UNUSED(argv)

MyObject *myObj = new MyObject;

QScriptEngine eng;
eng.globalObject().setProperty("obj", eng.newQObject(myObj));

qDebug() << eng.evaluate(


"obj.enabled = true;\n"
"print('Enabled: ' + obj.enabled);\n"
"print('isEnabled: ' + obj.isEnabled());"
).toString();
return 0;
}