TranslucentCloud TranslucentCloud - 3 months ago 47
C++ Question

Reuse existing QDirIterator in Qt 5

I use multiple

QDirIterator
s 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?

Answer

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();
  };
}