Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can I search for files via QtConcurrent?
QtWS25 Last Chance

Can I search for files via QtConcurrent?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RazrFalcon
    wrote on last edited by
    #1

    I need to find files on my HDD (recursively).
    But it takes a long of time and GUI freezes.
    I try to solve it via QtConcurrent:
    @void MainWindow::on_buttonSearch_clicked()
    {
    QFutureWatcher<QFileInfoList> watcher;
    connect(&watcher, SIGNAL(finished()), this, SLOT(loadFinished()));
    future = QtConcurrent::run(&MainWindow::searchForFiles, lineEditInDir->text()); // in .h QFuture<QFileInfoList> future;
    watcher.setFuture(future);
    }

    QFileInfoList MainWindow::searchForFiles(const QString &startDir)
    {
    QDir dir(startDir);
    QFileInfoList list;
    foreach (QString file, dir.entryList(QStringList("*.svg"), QDir::Files))
    list += QFileInfo(startDir+"/"+file);
    foreach (QString subdir, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot))
    list += searchForFiles(startDir+"/"+subdir);
    return list;
    }

    void MainWindow::loadFinished()
    {
    fileList = future.result();
    qDebug()<<fileList.count();
    }@

    All works fine, I think, but I can't cancel it. future.cancel() didn't work, because I use QtConcurrent::run().
    And i don't know how to put files search into QtConcurrent::mapped(), which can be canceled.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      I think it would be useless to use QtConcurrent for this. QtConcurrent is suitable for problems that are are easy to parallelize. Searching your HDD will have I/O as the bottleneck, so using multiple threads to do that is not efficient. The threads will end up waiting for each other.

      So, I would use either a QObject derived worker that you move to a vanilla [[doc:QThread]], or a [[doc:QRunnable]] subclass in conjunction with [[doc:QThreadPool]]. Because you seem to need cancel capabilities, I think the QThread route would be easiest.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        Using QtConcurrent can even worse the situation as OS caches might get flushed once the next thread is running and the data needs to be re-read if the first thread is active again.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved