Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Iterate over a QList with QFile's

Iterate over a QList with QFile's

Scheduled Pinned Locked Moved General and Desktop
5 Posts 5 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NeedDeen
    wrote on last edited by
    #1

    Hi

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

    Pseudocode:
    @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
    }@

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      Hi!

      Try:

      @if(!list.isEmpty())
      foreach(QFile *i, list)
      {
      //Your code here
      }@


      Code is poetry

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

        @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.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            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;@

            Greetz, Jeroen

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved