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.

4

1 回答 1

1

据我所知,运算符不能在 QtScript 中重载,因为一般 Javascript 中不允许重载(例如,参见ECMA Script 4 - Progress和本文

现在,对于您的情况,您可以选择使用 Add、Mult、... 函数或使用一些限制较少的脚本语言。

于 2010-08-04T18:47:57.613 回答