I'd probably go with QThread as well. Something like
{
Q_OBJECT
public:
: iterator
(dir, QDirIterator
::Subdirectories),
QThread(parent
), stopped
(false) {
}
void run()
{
while(!stopped && iterator.hasNext()) {
if(QFileInfo(entry
).
suffix().
toLower() == "png") { fileList << entry;
emit fileCountChanged(fileList.count());
}
}
}
{
return fileList;
}
public slots:
void stop()
{
stopped = true;
}
signals:
void fileCountChanged(int);
private:
QDirIterator iterator;
bool stopped;
};
class Scanner : public QThread
{
Q_OBJECT
public:
explicit Scanner(const QDir &dir, QObject *parent = 0)
: iterator(dir, QDirIterator::Subdirectories), QThread(parent), stopped(false)
{
}
void run()
{
while(!stopped && iterator.hasNext()) {
QString entry(iterator.next());
if(QFileInfo(entry).suffix().toLower() == "png") {
fileList << entry;
emit fileCountChanged(fileList.count());
}
}
}
QStringList files() const
{
return fileList;
}
public slots:
void stop()
{
stopped = true;
}
signals:
void fileCountChanged(int);
private:
QDirIterator iterator;
QStringList fileList;
bool stopped;
};
To copy to clipboard, switch view to plain text mode
Then simply start(), stop() and wait(), as in the original code snippet above.
Bookmarks