Qt 4.8
Public Functions | Properties | List of all members
QScriptValueIterator Class Reference

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

#include <qscriptvalueiterator.h>

Public Functions

QScriptValue::PropertyFlags flags () const
 Returns the flags of the last property that was jumped over using next() or previous(). More...
 
bool hasNext () const
 Returns true if there is at least one item ahead of the iterator (i.e. More...
 
bool hasPrevious () const
 Returns true if there is at least one item behind the iterator (i.e. More...
 
QString name () const
 Returns the name of the last property that was jumped over using next() or previous(). More...
 
void next ()
 Advances the iterator by one position. More...
 
QScriptValueIteratoroperator= (QScriptValue &value)
 Makes the iterator operate on object. More...
 
void previous ()
 Moves the iterator back by one position. More...
 
 QScriptValueIterator (const QScriptValue &value)
 Constructs an iterator for traversing object. More...
 
void remove ()
 Removes the last property that was jumped over using next() or previous(). More...
 
QScriptString scriptName () const
 Returns the name of the last property that was jumped over using next() or previous(). More...
 
void setValue (const QScriptValue &value)
 Sets the value of the last property that was jumped over using next() or previous(). More...
 
void toBack ()
 Moves the iterator to the back of the QScriptValue (after the last property). More...
 
void toFront ()
 Moves the iterator to the front of the QScriptValue (before the first property). More...
 
QScriptValue value () const
 Returns the value of the last property that was jumped over using next() or previous(). More...
 
 ~QScriptValueIterator ()
 Destroys the iterator. More...
 

Properties

QScopedPointer< QScriptValueIteratorPrivated_ptr
 

Detailed Description

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

Since
4.3

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()) {
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();
continue;
qDebug() << "found enumerated property:" << it.name();
}
See also
QScriptValue::property()

Definition at line 41 of file qscriptvalueiterator.h.

Constructors and Destructors

◆ QScriptValueIterator()

QScriptValueIterator::QScriptValueIterator ( const QScriptValue object)

Constructs an iterator for traversing object.

The iterator is set to be at the front of the sequence of properties (before the first property).

Definition at line 142 of file qscriptvalueiterator.cpp.

143  : d_ptr(0)
144 {
145  if (object.isObject()) {
147  d_ptr->objectValue = object;
148  }
149 }
QScopedPointer< QScriptValueIteratorPrivate > d_ptr
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.

◆ ~QScriptValueIterator()

QScriptValueIterator::~QScriptValueIterator ( )

Destroys the iterator.

Definition at line 154 of file qscriptvalueiterator.cpp.

155 {
156 }

Functions

◆ flags()

QScriptValue::PropertyFlags QScriptValueIterator::flags ( ) const

Returns the flags of the last property that was jumped over using next() or previous().

See also
value()

Definition at line 328 of file qscriptvalueiterator.cpp.

Referenced by QScriptObjectSnapshot::capture(), QScriptDebuggerCommandExecutor::execute(), expandObject(), and QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass().

329 {
330  Q_D(const QScriptValueIterator);
331  if (!d || !d->initialized || !d->engine())
332  return 0;
333  QScript::APIShim shim(d->engine());
334  return d->object()->propertyFlags(*d->current);
335 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ hasNext()

bool QScriptValueIterator::hasNext ( ) const

Returns true if there is at least one item ahead of the iterator (i.e.

the iterator is not at the back of the property sequence); otherwise returns false.

See also
next(), hasPrevious()

Definition at line 165 of file qscriptvalueiterator.cpp.

Referenced by FlatListModel::addValue(), QScriptObjectSnapshot::capture(), QDeclarativeComponentPrivate::createObject(), QScriptDebuggerCommandExecutor::execute(), expandObject(), QDeclarativeGlobalScriptClass::explicitSetProperty(), QJSDebuggerAgentPrivate::getLocals(), QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(), QDeclarativeWorkerScriptEnginePrivate::scriptValueToVariant(), NestedListModel::set(), and ModelNode::setObjectValue().

166 {
167  Q_D(const QScriptValueIterator);
168  if (!d || !d->engine())
169  return false;
170 
172  return d->it != d->propertyNames.end();
173 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
static void ensureInitialized()
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ hasPrevious()

bool QScriptValueIterator::hasPrevious ( ) const

Returns true if there is at least one item behind the iterator (i.e.

the iterator is not at the front of the property sequence); otherwise returns false.

See also
previous(), hasNext()

Definition at line 201 of file qscriptvalueiterator.cpp.

202 {
203  Q_D(const QScriptValueIterator);
204  if (!d || !d->engine())
205  return false;
206 
208  return d->it != d->propertyNames.begin();
209 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
static void ensureInitialized()
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ name()

QString QScriptValueIterator::name ( ) const

Returns the name of the last property that was jumped over using next() or previous().

See also
value(), flags()

Definition at line 265 of file qscriptvalueiterator.cpp.

Referenced by FlatListModel::addValue(), QScriptObjectSnapshot::capture(), QDeclarativeComponentPrivate::createObject(), QScriptDebuggerCommandExecutor::execute(), expandObject(), QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(), QDeclarativeWorkerScriptEnginePrivate::scriptValueToVariant(), NestedListModel::set(), and ModelNode::setObjectValue().

266 {
267  Q_D(const QScriptValueIterator);
268  if (!d || !d->initialized || !d->engine())
269  return QString();
270  return d->current->ustring();
271 }
double d
Definition: qnumeric_p.h:62
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ next()

void QScriptValueIterator::next ( )

Advances the iterator by one position.

Calling this function on an iterator located at the back of the container leads to undefined results.

See also
hasNext(), previous(), name()

Definition at line 183 of file qscriptvalueiterator.cpp.

Referenced by FlatListModel::addValue(), QScriptObjectSnapshot::capture(), QDeclarativeComponentPrivate::createObject(), QScriptDebuggerCommandExecutor::execute(), expandObject(), QDeclarativeGlobalScriptClass::explicitSetProperty(), QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(), QDeclarativeWorkerScriptEnginePrivate::scriptValueToVariant(), NestedListModel::set(), and ModelNode::setObjectValue().

184 {
186  if (!d)
187  return;
188  d->ensureInitialized();
189 
190  d->current = d->it;
191  ++(d->it);
192 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ operator=()

QScriptValueIterator & QScriptValueIterator::operator= ( QScriptValue object)

Makes the iterator operate on object.

The iterator is set to be at the front of the sequence of properties (before the first property).

Definition at line 358 of file qscriptvalueiterator.cpp.

359 {
360  d_ptr.reset();
361  if (object.isObject()) {
363  d_ptr->objectValue = object;
364  }
365  return *this;
366 }
QScopedPointer< QScriptValueIteratorPrivate > d_ptr
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.

◆ previous()

void QScriptValueIterator::previous ( )

Moves the iterator back by one position.

Calling this function on an iterator located at the front of the container leads to undefined results.

See also
hasPrevious(), next(), name()

Definition at line 219 of file qscriptvalueiterator.cpp.

220 {
222  if (!d)
223  return;
224  d->ensureInitialized();
225  --(d->it);
226  d->current = d->it;
227 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ remove()

void QScriptValueIterator::remove ( )

Removes the last property that was jumped over using next() or previous().

See also
setValue()

Definition at line 343 of file qscriptvalueiterator.cpp.

344 {
346  if (!d || !d->initialized || !d->engine())
347  return;
348  QScript::APIShim shim(d->engine());
349  d->object()->setProperty(*d->current, JSC::JSValue());
350  d->propertyNames.erase(d->current);
351 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ scriptName()

QScriptString QScriptValueIterator::scriptName ( ) const

Returns the name of the last property that was jumped over using next() or previous().

Since
4.4

Definition at line 282 of file qscriptvalueiterator.cpp.

Referenced by QDeclarativeGlobalScriptClass::explicitSetProperty(), and QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass().

283 {
284  Q_D(const QScriptValueIterator);
285  if (!d || !d->initialized || !d->engine())
286  return QScriptString();
287  return d->engine()->toStringHandle(*d->current);
288 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptString class acts as a handle to "interned" strings in a QScriptEngine.
Definition: qscriptstring.h:38
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ setValue()

void QScriptValueIterator::setValue ( const QScriptValue value)

Sets the value of the last property that was jumped over using next() or previous().

See also
value(), name()

Definition at line 312 of file qscriptvalueiterator.cpp.

313 {
315  if (!d || !d->initialized || !d->engine())
316  return;
317  QScript::APIShim shim(d->engine());
318  JSC::JSValue jsValue = d->engine()->scriptValueToJSCValue(value);
319  d->object()->setProperty(*d->current, jsValue);
320 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ toBack()

void QScriptValueIterator::toBack ( )

Moves the iterator to the back of the QScriptValue (after the last property).

See also
toFront(), previous()

Definition at line 250 of file qscriptvalueiterator.cpp.

251 {
253  if (!d)
254  return;
255  d->ensureInitialized();
256  d->it = d->propertyNames.end();
257 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ toFront()

void QScriptValueIterator::toFront ( )

Moves the iterator to the front of the QScriptValue (before the first property).

See also
toBack(), next()

Definition at line 235 of file qscriptvalueiterator.cpp.

236 {
238  if (!d)
239  return;
240  d->ensureInitialized();
241  d->it = d->propertyNames.begin();
242 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.

◆ value()

QScriptValue QScriptValueIterator::value ( ) const

Returns the value of the last property that was jumped over using next() or previous().

See also
setValue(), name()

Definition at line 296 of file qscriptvalueiterator.cpp.

Referenced by FlatListModel::addValue(), QScriptObjectSnapshot::capture(), QDeclarativeComponentPrivate::createObject(), QScriptDebuggerCommandExecutor::execute(), expandObject(), QDeclarativeGlobalScriptClass::explicitSetProperty(), QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(), QDeclarativeWorkerScriptEnginePrivate::scriptValueToVariant(), and ModelNode::setObjectValue().

297 {
298  Q_D(const QScriptValueIterator);
299  if (!d || !d->initialized || !d->engine())
300  return QScriptValue();
301  QScript::APIShim shim(d->engine());
302  JSC::JSValue jsValue = d->object()->property(*d->current);
303  return d->engine()->scriptValueFromJSCValue(jsValue);
304 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57

Properties

◆ d_ptr

QScopedPointer<QScriptValueIteratorPrivate> QScriptValueIterator::d_ptr
private

Definition at line 69 of file qscriptvalueiterator.h.

Referenced by operator=(), and QScriptValueIterator().


The documentation for this class was generated from the following files: