Results 1 to 2 of 2

Thread: override operator with qtscript

  1. #1
    Join Date
    Jun 2010
    Posts
    38
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question override operator with qtscript

    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
    Qt Code:
    1. class ByteArrayClass : public QObject, public QScriptClass
    2. {
    3. public:
    4. QByteArray &operator+(int n);
    5. }
    6.  
    7. main.cpp
    8. int main(int argc, char **argv)
    9. {
    10. QCoreApplication app(argc, argv);
    11. QScriptEngine eng;
    12. ByteArrayClass *baClass = new ByteArrayClass(&eng);
    13. eng.globalObject().setProperty("ByteArray", baClass->constructor());
    14. eng.evaluate("ba = new ByteArray(4))"
    15. eng.evaluate("ba+2;"); //this will not invoke override operator+.
    16.  
    17. ByteArrayClass *ba = new ByteArrayClass(&eng);
    18. int n = 3;
    19. *ba + n; //but this can invoke the override operator+
    20. }
    To copy to clipboard, switch view to plain text mode 

    If this couldn't be realised,maybe the one way is to replace all the operator to the custom function.
    Last edited by wookoon; 20th July 2010 at 10:14.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: override operator with qtscript

    QtScript knows nothing about your operator so it can't invoke it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Calling object's operator+ from QtScript
    By PolyVox in forum Qt Programming
    Replies: 1
    Last Post: 1st July 2010, 22:37
  2. Shortcut override ?
    By fenerista in forum Qt Programming
    Replies: 6
    Last Post: 23rd May 2010, 12:02
  3. Override a signal
    By daviddoria in forum Newbie
    Replies: 4
    Last Post: 9th February 2010, 18:18
  4. Replies: 0
    Last Post: 25th November 2009, 07:46
  5. QTextEdit setText override?
    By ksierens in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2006, 17:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.