
Originally Posted by
wysota
But you are not using
QVector here anywhere. There is just javascript code here.
I have try another method:
I write a object and overide the "+" operator,and make it availiable in qtscript,
{
Q_OBJECT
public:
explicit MyObject
(QObject *parent
= 0);
Q_INVOKABLE MyObject* operator+(const MyObject &other)
{
MyObject *object = new MyObject;
object->d = other.d + this->d;
return object;
};
void setValue(int value){d = value;};
int value(){return d;};
private:
int d;
};
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = 0);
Q_INVOKABLE MyObject* operator+(const MyObject &other)
{
MyObject *object = new MyObject;
object->d = other.d + this->d;
return object;
};
void setValue(int value){d = value;};
int value(){return d;};
private:
int d;
};
To copy to clipboard, switch view to plain text mode
main.cpp:
MyObject * object = new MyObject;
object->setValue(10);
QScriptValue objectValue = engine->newQObject(object);
engine->globalObject().setProperty("obj1", objectValue);
MyObject * object2 = new MyObject;
object2->setValue(5);
objectValue = engine->newQObject(object2);
engine->globalObject().setProperty("obj2", objectValue);
engine->evaluate("obj1+obj2;");
MyObject * object = new MyObject;
object->setValue(10);
QScriptValue objectValue = engine->newQObject(object);
engine->globalObject().setProperty("obj1", objectValue);
MyObject * object2 = new MyObject;
object2->setValue(5);
objectValue = engine->newQObject(object2);
engine->globalObject().setProperty("obj2", objectValue);
engine->evaluate("obj1+obj2;");
To copy to clipboard, switch view to plain text mode
But from my debug, the "obj1 + obj2" did not execute the MyObject's "+" operator.
So this method maybe is no effect.
Bookmarks