views:

34

answers:

1

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.

A: 

As far as I know operators can not be overloaded in QtScript, because it is not allowed in Javascript in general (e.g. see ECMA Script 4 - Progress and this Article).

Now for your case you have the choice to go with Add, Mult, ... functions or leave for some less constrained scripting language.

FFox