Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Iterate over a QList with QFile's

    General and Desktop
    5
    5
    5648
    Loading More Posts
    • 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
      NeedDeen last edited by

      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 Reply Quote 0
      • J
        Jake007 last edited by

        Hi!

        Try:

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


        Code is poetry

        1 Reply Last reply Reply Quote 0
        • M
          mlong last edited by

          @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 Reply Quote 0
          • D
            DerManu last edited by

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

            1 Reply Last reply Reply Quote 0
            • Jeroentjehome
              Jeroentjehome last edited by

              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 Reply Quote 0
              • First post
                Last post