tags:

views:

39

answers:

2

I can't dereference a QMutableListIterator like an STL iterator - with *it.

I'm trying to use QMutableListIterator::value() but my program crashes. What is the right way to do this?

QFileInfoList files;
// populate list
QListIterator<QFileInfo> it(files);
it.toFront();
QFileInfo = it_top.value();
// crash

The error is

ASSERT: "item_exists()" in file /usr/include/qt4/QtCore/qlist.h, line 778

Thanks

+1  A: 

Hello MVG, I just checked the Qt documentation for "toFront" and it says:

Moves the iterator to the front of the container (before the first item).

http://doc.trolltech.com/latest/qmutablelistiterator.html#toFront

The Qt Iterators are Java-style iterators which start before the items and end on the last item, the C++ style iterators begin on the first item and end after the last one.

Haplo
A: 

There is nothing wrong with the code you have shown.

The problem actually lies in // populate list

Actually I guess no item gets populated in the QFileInfoList. That's why you are getting the ASSERT when you try to access it.

And check for the hasNext() return value. Most possibly it will return false.

Hope it helps in someway..

liaK
Hey, thanks. hasNext() works fine, and the list is definitely populated ok. @Haplo's assessment is right, I think
MVG