Results 1 to 5 of 5

Thread: QtScript : passing array as argument into function

  1. #1
    Join Date
    Sep 2007
    Location
    Vancouver B.C., Canada
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QtScript : passing array as argument into function

    Greetings,

    I've been playing around with the newer QtScript functionality from Qt 4.3, and right now I'm trying to figure out the best way to allow me to modify a QByteArray in a script function.

    Preferrably it would be something treated as a normal script Array (so you can use the [] to access elements).

    By subclassing QByteArray and adding Q_OBJECT, I've been able to get a class that I can use QScriptEngine::newQObject() with, and pass as a QScriptValue into the script function (to which I can access the methods designated as 'slots').

    From some testing it would appear that I cannot make 'operator[]' a slot (compiler gives an error 'Not a signal or slot declaration'); so my question is, is there a way to achieve a normal array functionality through a passed-in QByteArray (or some derivative thereof)?

    Any ideas/insight would be appreciated!

    Thanks,
    Derek R.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: QtScript : passing array as argument into function

    You can provide get/set methods for the class.

  3. #3
    Join Date
    Sep 2007
    Location
    Vancouver B.C., Canada
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QtScript : passing array as argument into function

    Quote Originally Posted by wysota View Post
    You can provide get/set methods for the class.
    Sorry I should have given a bit more detail, I currently have get/set methods to allow me to get/set elements in the QByteArray however I was trying to find a way to provide 'operator[]' functionality in the script.

    Currently I have this:

    Qt Code:
    1. function test(arg) {
    2. print("arg[0] = " + arg.getAt(0) + "\n");
    3. arg.setAt(0,123);
    4. }
    To copy to clipboard, switch view to plain text mode 

    What I'm trying to achieve is this:

    Qt Code:
    1. function test(arg) {
    2. print("arg[0] = " + arg[0] + "\n");
    3. arg[0] = 123;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is the latter possible with QtScript?


    Derek R.

  4. #4
    Join Date
    Sep 2007
    Location
    Vancouver B.C., Canada
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QtScript : passing array as argument into function

    I still haven't been able to find a solution to the operator[] question so I've just been doing tests using my set/get methods for array access.

    The problem I'm running into now is the speed of accessing the array, just a relatively small array size of 100000 entries takes ~3s to iterate through and set.

    I was expecting maybe a few orders of magnitude difference in speed (versus normal C++ execution), however this is more like several orders of magnitude difference. Is there any way to speed this up, or a different way to pass in and modify a byte array in a script function?

    Derek R.

  5. #5
    Join Date
    Jan 2007
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtScript : passing array as argument into function

    Are there any news concerning your approach?

    If you only want to use the operator[], something like this may help:

    Qt Code:
    1. QScriptEngine *engine = new QScriptEngine();
    2. QByteArray byteArray;
    3.  
    4. QScriptValue array = engine->newArray(byteArray.size())
    5. for (int i = 0; i < byteArray.size(); ++i)
    6. array.setProperty(i, QScriptValue(engine, byteArray[i]);
    To copy to clipboard, switch view to plain text mode 

    The QScriptValue can then be handed over to the engine. However, the drawbacks of this approach are that you have to hard-copy the whole QByteArray and that it will not work with your subclass-method (at least I cannot see how ).

    To copy the QScriptValue back to the QByteArray, try something like this:

    Qt Code:
    1. QByteArray byteArray;
    2. uint len = array.property("length").toUInt32();
    3. byteArray.resize(len);
    4. for (uint i = 0; i < len; ++i)
    5. byteArray[i] = values.property(i).toInt32());
    To copy to clipboard, switch view to plain text mode 

    I hope this helps, lucky coding
    Last edited by flare; 27th October 2007 at 11:46.

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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.