PDA

View Full Version : override operator with qtscript



wookoon
20th July 2010, 10:08
I want do a math editor using qtscript.
It will support array calculating in script. Such as array1 + array2 = array3.({1,2,3}+{3,4,5} = {4,6,8});
Maybe I need override operator+,
I consult the example of QByteArray, and I override operator+,but when I execute in Script,it can't be invoke,anyone coule give me some suggestions?

bytearray.h


class ByteArrayClass : public QObject, public QScriptClass
{
public:
QByteArray &operator+(int n);
}

main.cpp
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QScriptEngine eng;
ByteArrayClass *baClass = new ByteArrayClass(&eng);
eng.globalObject().setProperty("ByteArray", baClass->constructor());
eng.evaluate("ba = new ByteArray(4))"
eng.evaluate("ba+2;"); //this will not invoke override operator+.

ByteArrayClass *ba = new ByteArrayClass(&eng);
int n = 3;
*ba + n; //but this can invoke the override operator+
}


If this couldn't be realised,maybe the one way is to replace all the operator to the custom function.

wysota
20th July 2010, 10:29
QtScript knows nothing about your operator so it can't invoke it.