PDA

View Full Version : how to implement QScriptValueIterator?



TorAn
2nd December 2012, 15:57
I don't understand how to implement QScriptValueIterator. I want to implement a script object that has some properties and also allows for java-style iteration in the script text.

Documentation (http://doc.qt.digia.com/qt/qscriptvalue.html#details) states:
"If you want to iterate over the properties of a script object, use the QScriptValueIterator class."

My code looks like this:

static void registerWithScript(const char* scriptObjName, QScriptEngine& scriptengine)
{
T* t = new T();
QScriptValue obj = scriptengine.newQObject(t, QScriptEngine::ScriptOwnership);
scriptengine.setDefaultPrototype(qMetaTypeId<T>(),obj);

QScriptValue ctor = scriptengine.newFunction(objectConst);
QScriptValue metaObject = scriptengine.newQMetaObject(&QObject::staticMetaObject, ctor);
scriptengine.globalObject().setProperty(scriptObjN ame, metaObject);
}

Constructor of QScriptValueIterator requires QScriptValue object to be already created.

So, if I modify my code to add


QScriptValueIterator iter (obj);
after constructing the obj, the iterator seems to exist outside the scriptengine. Right?
Who implements all these getNext, getPrev etc methods of QScriptValueIterator?

Thanks.

amleto
2nd December 2012, 16:16
why do you want to implement it? It's already implemented...

http://qt-project.org/doc/qt-4.8/qscriptvalueiterator.html

TorAn
2nd December 2012, 18:43
I don't undestand how QScriptValueIterator should be used. Exampe:

I have class foo.


class foo : public QObject
{
Q_PROPERTY(int size, READ getsize);
...
private
std::vector<int> _coll
};
I can expose it to QtScript and then write a javascript function to be evaluated using foo instance

function test()
{
var myfoo = new foo();
var icount = myfoo.size;
}Now I'd like to be able to iterate though the _coll member of foo. I can write custom methods and properties to do so, but there is also QScriptValueIterator in QtScript module. How (if at all) it should be used in this situation?

I'll appreciate code example. Thanks.

amleto
2nd December 2012, 18:58
_coll isnt a property...

TorAn
2nd December 2012, 19:33
amleto: I know that! ((c) Forrest Gump).

I don't know how to make my question any clearer, but let me try again.

Using the class foo from my previous message: how to expose _coll member of class foo to the scripting environment using QScriptValueIterator?

amleto
2nd December 2012, 19:43
err... make it a property

anda_skoa
3rd December 2012, 00:24
QScriptValueIterator is a helper for the C++ side, i.e. to iterate over an QScriptValue instance's properties.

It is not for implementing iterators on the script side.

What you might want to use is qScriptRegisterSequenceMetaType and exposing your container as a propery
http://qt-project.org/doc/qt-4.8/qscriptengine.html#qScriptRegisterSequenceMetaType

Cheers,
_