View Full Version : Qt3 to Qt4 with QListIterator<AbstractThing *> it() with QPushbutton

23rd June 2011, 03:23

I am converting a Qt3 App to Qt4 (Qt4.7.1).

I have a class:

class AbstractThing : public QPushButton

In Qt3 I had a

class NodeManager : public QObject, QPtrList<AbstractThing>

I have converted this in Qt4 to

class NodeManager : public QObject, QList<AbstractThing*>

I have a function in NodeManager that looks at the items in the QList, in Qt3 I had

void NodeManager::doStuff()

AbstractThing *thing;

for(thing = first(); thing; thing = next())

In Qt4 next() is no longer in Qlist so i have decided to use a QListIterator

QListIterator<AbstractThing *> it();

AbstractThing *thing = first();

while (it.hasNext())

But what do i put in the empty ()? I can't put 'this'

Or will i be better doing a count on the list and a for(int i = 0 .... with item.at(i) etc...

Thank you

Santosh Reddy
23rd June 2011, 06:36
QListIterator is a Java-style const iterator, which does not require empty(), instead hasNext() has to be used.

moreover empty() is supposed to be operated on QList, where as hasNext() is supposed to be operated on QListIterator

There may be many ways to iterate the list, here are 3 possible ways

1. Java-style iterators (you already use this, fine)
2. STL-style iterators (slightly faster and more cumbersome to use to than Java-style iterators

QList<AbstractThing *>::const_iterator i;
for (i = this->constBegin(); i != this->constEnd(); ++i)...*i...
3. Plain index based

for(int i = 0; i < this->size(); i++)...this.at(i)....