user6000109
user6000109

Reputation:

Recursively iterating through directory using QDirIterator

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.

Upvotes: 1

Views: 767

Answers (1)

Alexander V
Alexander V

Reputation: 8718

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

Upvotes: 0

Related Questions