how to implement QScriptValueIterator?
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 states:
"If you want to iterate over the properties of a script object, use the QScriptValueIterator class."
My code looks like this:
Code:
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(scriptObjName, metaObject);
}
Constructor of QScriptValueIterator requires QScriptValue object to be already created.
So, if I modify my code to add
Code:
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.
Re: how to implement QScriptValueIterator?
why do you want to implement it? It's already implemented...
http://qt-project.org/doc/qt-4.8/qsc...eiterator.html
Re: how to implement QScriptValueIterator?
I don't undestand how QScriptValueIterator should be used. Exampe:
I have class foo.
Code:
{
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
Code:
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.
Re: how to implement QScriptValueIterator?
Re: how to implement QScriptValueIterator?
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?
Re: how to implement QScriptValueIterator?
err... make it a property
Re: how to implement QScriptValueIterator?
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/qsc...quenceMetaType
Cheers,
_