PDA

View Full Version : QtScript : passing array as argument into function



derek_r
29th September 2007, 17:48
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.

wysota
29th September 2007, 18:34
You can provide get/set methods for the class.

derek_r
29th September 2007, 19:33
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:


function test(arg) {
print("arg[0] = " + arg.getAt(0) + "\n");
arg.setAt(0,123);
}

What I'm trying to achieve is this:


function test(arg) {
print("arg[0] = " + arg[0] + "\n");
arg[0] = 123;
}

Is the latter possible with QtScript?


Derek R.

derek_r
1st October 2007, 06:46
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.

flare
27th October 2007, 11:46
Are there any news concerning your approach?

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


QScriptEngine *engine = new QScriptEngine();
QByteArray byteArray;

QScriptValue array = engine->newArray(byteArray.size())
for (int i = 0; i < byteArray.size(); ++i)
array.setProperty(i, QScriptValue(engine, byteArray[i]);

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:


QByteArray byteArray;
uint len = array.property("length").toUInt32();
byteArray.resize(len);
for (uint i = 0; i < len; ++i)
byteArray[i] = values.property(i).toInt32());

I hope this helps, lucky coding :)