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. [SOLVED] Reuse a QDirIterator

[SOLVED] Reuse a QDirIterator

Scheduled Pinned Locked Moved General and Desktop
beginnerc++
8 Posts 3 Posters 3.4k 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.
  • H Offline
    H Offline
    Hazzl
    wrote on last edited by Hazzl
    #1

    I would like to assign a new directory to a QDirIterator that I have allocated in my constructor with an empty directory. I want to do that to avoid having to deal with memory management. I only need one Iterator at a time and would like to repurpose one variable.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QDirIterator does not expose such api, but I wouldn't worry that much about it. It's a fairly lightweight class. I wouldn't make an instance of it part of your class. Just allocate it on the stack whenever you need it.

      1 Reply Last reply
      1
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Like @Chris-Kawa said: Create it when you need it. But if you really always want to have a copy around and you're worried about memory management then use a smart pointer:

        std::unique_ptr<QDirIterator> m_it; // member variable
        // ...
          , m_it( new QDirIterator("") ) // initialize in constructor
        // ...
        m_it.reset( new QDirIterator("/home", QDirIterator::Subdirectories) );
        while (m_it->hasNext()) {
             qDebug() << m_it->next();
        }
        
        1 Reply Last reply
        1
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Wieland That's still recreating the iterator every time, just placed in an extra wrapper. It's basically like std::unique_ptr<QString>. There's no point to do it whatsoever.

          ? 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            @Wieland That's still recreating the iterator every time, just placed in an extra wrapper. It's basically like std::unique_ptr<QString>. There's no point to do it whatsoever.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            @Chris-Kawa The point is that he can reuse the iterator in case he doesn't need to change the path always. But I wouldn't do it.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              This wouldn't reuse the iterator. It is recreated by this m_it.reset( new QDirIterator("/home", QDirIterator::Subdirectories) );. The pointer is the same, but the iterator is a whole new instance. If the path doesn't change it's still better to store it (e.g. in QString) and use a stack based instance with it than to store that pointer and recreate the iterator anyways.

              ? 1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                This wouldn't reuse the iterator. It is recreated by this m_it.reset( new QDirIterator("/home", QDirIterator::Subdirectories) );. The pointer is the same, but the iterator is a whole new instance. If the path doesn't change it's still better to store it (e.g. in QString) and use a stack based instance with it than to store that pointer and recreate the iterator anyways.

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @Chris-Kawa Yes, of course.

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  Hazzl
                  wrote on last edited by
                  #8

                  Thanks a lot for your pointers (no pun intended :-)

                  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