@@ -219,7 +219,7 @@ void FileSystemModel::watchDirIfRecursive(const QString& path) {
219219 if (m_recursive && m_watchChanges) {
220220 const auto currentDir = m_dir;
221221 const bool showHidden = m_showHidden;
222- auto future = QtConcurrent::run ([showHidden, path]() {
222+ const auto future = QtConcurrent::run ([showHidden, path]() {
223223 QDir::Filters filters = QDir::Dirs | QDir::NoDotAndDotDot;
224224 if (showHidden) {
225225 filters |= QDir::Hidden;
@@ -232,12 +232,16 @@ void FileSystemModel::watchDirIfRecursive(const QString& path) {
232232 }
233233 return dirs;
234234 });
235- future.then (this , [currentDir, showHidden, this ](const QStringList& paths) {
235+ const auto watcher = new QFutureWatcher<QStringList>(this );
236+ connect (watcher, &QFutureWatcher<QStringList>::finished, this , [currentDir, showHidden, watcher, this ]() {
237+ const auto paths = watcher->result ();
236238 if (currentDir == m_dir && showHidden == m_showHidden && !paths.isEmpty ()) {
237239 // Ignore if dir or showHidden has changed
238240 m_watcher.addPaths (paths);
239241 }
242+ watcher->deleteLater ();
240243 });
244+ watcher->setFuture (future);
241245 }
242246}
243247
@@ -291,7 +295,7 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) {
291295 oldPaths << entry->path ();
292296 }
293297
294- auto future = QtConcurrent::run ([=](QPromise<QPair<QSet<QString>, QSet<QString>>>& promise) {
298+ const auto future = QtConcurrent::run ([=](QPromise<QPair<QSet<QString>, QSet<QString>>>& promise) {
295299 const auto flags = recursive ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags;
296300
297301 std::optional<QDirIterator> iter;
@@ -349,7 +353,7 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) {
349353 newPaths.insert (path);
350354 }
351355
352- if (promise.isCanceled ()) {
356+ if (promise.isCanceled () || newPaths == oldPaths ) {
353357 return ;
354358 }
355359
@@ -361,17 +365,23 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) {
361365 }
362366 m_futures.insert (dir, future);
363367
364- future
365- .then (this ,
366- [dir, this ](QPair<QSet<QString>, QSet<QString>> result) {
367- m_futures.remove (dir);
368- if (!result.first .isEmpty () || !result.second .isEmpty ()) {
369- applyChanges (result.first , result.second );
370- }
371- })
372- .onCanceled (this , [dir, this ]() {
373- m_futures.remove (dir);
374- });
368+ const auto watcher = new QFutureWatcher<QPair<QSet<QString>, QSet<QString>>>(this );
369+
370+ connect (watcher, &QFutureWatcher<QPair<QSet<QString>, QSet<QString>>>::finished, this , [dir, watcher, this ]() {
371+ m_futures.remove (dir);
372+
373+ if (!watcher->future ().isResultReadyAt (0 )) {
374+ watcher->deleteLater ();
375+ return ;
376+ }
377+
378+ const auto result = watcher->result ();
379+ applyChanges (result.first , result.second );
380+
381+ watcher->deleteLater ();
382+ });
383+
384+ watcher->setFuture (future);
375385}
376386
377387void FileSystemModel::applyChanges (const QSet<QString>& removedPaths, const QSet<QString>& addedPaths) {
0 commit comments