Iterate over a QList with QFile's

  • Hi

    Is there an easy way to iterate over a QFile QList like this. I need to check, if a new item already exists:

    @QList<QFile *> list;

    void addItem(QFile *file)
    for(Begin List; Until End; One item)
    if(compare file with the QFile item from the current iteration) return;
    // Add item

  • Hi!


    foreach(QFile *i, list)
    //Your code here

  • Moderators

    @foreach(QFile *i, list)
    //Your code here
    }@ would be sufficient. If the list were empty, the foreach wouldn't loop at all.

    Edited to add:

    I agree with the posts following this one. There is a much better way to solve the original problem. I was just commenting on the post directly above.

  • Much easier:
    @ if (!list.contains(file))

  • Be aware that the foreach is much more code consuming then the C-style for loop, but if it isn't used a lot, the foreach is the way to go ;-)
    The correct use of the iterator is more like this:
    @QList<QFile *>::iterator i;@

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.