1

I'm trying to figure out a way to recursively iterate through a directory that is actively getting new files added to it as the program is running. QDirIterator works well to do the initial iteration but it seems there is no way to "refresh" QDirIterator so that it iterates through the directory again and comes up with an updated list of entries. Is there a way to make QDirIterator "refresh" or is there a better way to do what I am trying to accomplish?

Here's a snippet from my code:

QDirIterator dirIterator(directory,  QDir::AllEntries);

void MainWindow::on_Button_clicked()
{    
    if (dirIterator.hasNext()) {
        qDebug() << dirIterator.next();
        dirIterator.next();ui->imageChip1->setIcon(QIcon(dirIterator.next()));
            filename = dirIterator.fileName();
            filepath = dirIterator.filePath();
    }
}

This code works for any entries that existed in the directory when QDirIterator was initialized but NOT for any entries added to the directory AFTER QDirIterator was initialized.

1 Answers1

0

Is there a way to make QDirIterator "refresh" or is there a better way to do what I am trying to accomplish?

The problem is that with the scan filling the iterator object with directories and files structure you cannot get new file system changes. But you can definitely find help in Qt. It is called QFileSystemWatcher. To implement the functionality like you want I would first scan for directories and add them (or maybe just a root, depends) to watch. The simple example for watching just a directory: How to use QFileSystemWatcher to monitor a folder for change

Community
  • 1
  • 1
Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • Thanks. @AlexanderVX I wrote a function that notifies me every time the directory is modified but is there a way to update the QDirIterator when that happens to include the new files? –  Mar 01 '16 at 15:45
  • @CalypsoCoder, QDirIterator appears to be immutable. Making it to react on changes in the source of its data make it not immutable or fundamentally different. You could use the same interface from QDirIterator and make some hybrid with QFileSystemWatcher. That worth a couple of hours of work I guess and the interface needs to be extended with signals as well. – Alexander V Mar 01 '16 at 16:18