<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Can I search for files via QtConcurrent?]]></title><description><![CDATA[<p dir="auto">I need to find files on my HDD (recursively).<br />
But it takes a long of time and GUI freezes.<br />
I try to solve it via QtConcurrent:<br />
@void MainWindow::on_buttonSearch_clicked()<br />
{<br />
QFutureWatcher&lt;QFileInfoList&gt; watcher;<br />
connect(&amp;watcher, SIGNAL(finished()), this, SLOT(loadFinished()));<br />
future = QtConcurrent::run(&amp;MainWindow::searchForFiles, lineEditInDir-&gt;text());  // in .h QFuture&lt;QFileInfoList&gt; future;<br />
watcher.setFuture(future);<br />
}</p>
<p dir="auto">QFileInfoList MainWindow::searchForFiles(const QString &amp;startDir)<br />
{<br />
QDir dir(startDir);<br />
QFileInfoList list;<br />
foreach (QString file, dir.entryList(QStringList("*.svg"), QDir::Files))<br />
list += QFileInfo(startDir+"/"+file);<br />
foreach (QString subdir, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot))<br />
list += searchForFiles(startDir+"/"+subdir);<br />
return list;<br />
}</p>
<p dir="auto">void MainWindow::loadFinished()<br />
{<br />
fileList = future.result();<br />
qDebug()&lt;&lt;fileList.count();<br />
}@</p>
<p dir="auto">All works fine, I think, but I can't cancel it. future.cancel() didn't work, because I use QtConcurrent::run().<br />
And i don't know how to put files search into QtConcurrent::mapped(), which can be canceled.</p>
]]></description><link>https://forum.qt.io/topic/15062/can-i-search-for-files-via-qtconcurrent</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 22:11:50 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/15062.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 17 Mar 2012 18:46:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Can I search for files via QtConcurrent? on Mon, 19 Mar 2012 11:34:48 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/132927</link><guid isPermaLink="true">https://forum.qt.io/post/132927</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 19 Mar 2012 11:34:48 GMT</pubDate></item><item><title><![CDATA[Reply to Can I search for files via QtConcurrent? on Mon, 19 Mar 2012 10:37:46 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">So, I would use either a QObject derived worker that you move to a vanilla &lsqb;&lsqb;doc:QThread&rsqb;&rsqb;, or a &lsqb;&lsqb;doc:QRunnable&rsqb;&rsqb; subclass in conjunction with &lsqb;&lsqb;doc:QThreadPool&rsqb;&rsqb;. Because you seem to need cancel capabilities, I think the QThread route would be easiest.</p>
]]></description><link>https://forum.qt.io/post/132922</link><guid isPermaLink="true">https://forum.qt.io/post/132922</guid><dc:creator><![CDATA[andre]]></dc:creator><pubDate>Mon, 19 Mar 2012 10:37:46 GMT</pubDate></item></channel></rss>