PDA

View Full Version : QScript + SimpleClass not working



tdt
14th February 2010, 08:25
Hi all,
i have simple class which is used in QScript



//-------------------------------- myobject.h----------------------------
class MyObject : public QObject
{
Q_OBJECT
public:

MyObject(QObject *parent = 0);

Q_SCRIPTABLE int intern;
Q_INVOKABLE QString name();
Q_INVOKABLE int add(int b=0);

Q_INVOKABLE bool operator==(const MyObject &second) const;
Q_INVOKABLE MyObject operator+(int b);

~MyObject();
};
//-------------------------------- myobject.cpp----------------------------
MyObject::MyObject(QObject *parent)
: QObject(parent)
{
this->intern=0;
}
MyObject::~MyObject()
{
};
QString MyObject::name()
{ return QString("MyObject");
};
int MyObject::add(int b)
{ this->intern=this->intern+b;
return this->intern;
};

bool MyObject::operator==(const MyObject &second) const
{ return this->intern==second.intern;
};
MyObject MyObject::operator+(int b)
{ this->intern=this->intern+b;
return this;
} ;

and executing code


Q_SCRIPT_DECLARE_QMETAOBJECT(MyObject, QObject*);
...
QScriptEngine eng;


QScriptValue fClass = eng.scriptValueFromQMetaObject<MyObject>();
eng.globalObject().setProperty("MyObject", fClass);

qDebug()<<"var a=new MyObject()"<<eng.evaluate("var a=new MyObject()").toString();
qDebug()<<"a.name()"<<eng.evaluate("a.name()").toString();
qDebug()<<"a.intern=3"<<eng.evaluate("a.intern=3").toString();
qDebug()<<"a.intern"<<eng.evaluate("a.intern").toString();
qDebug()<<"a.add(5)"<<eng.evaluate("a.add(5)").toString();
qDebug()<<"a.intern"<<eng.evaluate("a.intern").toString();

In my opinion should be variable a represents class MyCObject and a.intern should be 3+5
it is not
is it right or bug? please send any idea you have

thanks
TDT

PS: is it possible override operator+ inside qscript?
----------------------------------------------------------
working on wxp, VS express 2008+QT4.5.0