Search code examples
c++qtdestroyreusability

Reuse existing QDirIterator in Qt 5


I use multiple QDirIterators in a single scope of a Qt 5 project. They typically look like this:

QDirIterator i(QDir::currentPath(), QDir::Dirs | QDir::NoDotAndDotDot);
while (i.hasNext()) {
    doSomething();
};

Now I use multiple objects all with theirs own names (i0, i1, i2 etc.), and I wonder how can I use just one name across the project, i in this example? How should I decommission existing QDirIterator to reuse it?


Solution

  • C++ allows you to introduce a new scope anywhere you need, for example:

    {
      QDirIterator i(path1, QDir::Dirs | QDir::NoDotAndDotDot);
      while (i.hasNext()) {
        doSomething();
      };
    }
    {
      QDirIterator i(path2, QDir::Dirs | QDir::NoDotAndDotDot);
      while (i.hasNext()) {
        doSomething();
      };
    }