QScriptValueIterator Class

The QScriptValueIterator class provides a Java-style iterator for QScriptValue. More...

Header: #include <QScriptValueIterator>
qmake: QT += script
Since: Qt 4.3

This class was introduced in Qt 4.3.

Detailed Description

The QScriptValueIterator constructor takes a QScriptValue as argument. After construction, the iterator is located at the very beginning of the sequence of properties. Here's how to iterate over all the properties of a QScriptValue:

 QScriptValue object;
 ...
 QScriptValueIterator it(object);
 while (it.hasNext()) {
     it.next();
     qDebug() << it.name() << ": " << it.value().toString();
 }

The next() advances the iterator. The name(), value() and flags() functions return the name, value and flags of the last item that was jumped over.

If you want to remove properties as you iterate over the QScriptValue, use remove(). If you want to modify the value of a property, use setValue().

Note that QScriptValueIterator only iterates over the QScriptValue's own properties; i.e. it does not follow the prototype chain. You can use a loop like this to follow the prototype chain:

 QScriptValue obj = ...; // the object to iterate over
 while (obj.isObject()) {
     QScriptValueIterator it(obj);
     while (it.hasNext()) {
         it.next();
         qDebug() << it.name();
     }
     obj = obj.prototype();
 }

Note that QScriptValueIterator will not automatically skip over properties that have the QScriptValue::SkipInEnumeration flag set; that flag only affects iteration in script code. If you want, you can skip over such properties with code like the following:

 while (it.hasNext()) {
     it.next();
     if (it.flags() & QScriptValue::SkipInEnumeration)
         continue;
     qDebug() << "found enumerated property:" << it.name();
 }

See also QScriptValue::property().