void SearchWidget::searchFiles(QDir & dir){ QStringList lstDirs = dir.entryList(QDir::Dirs | QDir::AllDirs | QDir::NoDotAndDotDot); foreach (QString entry, lstDirs) { QString entryAbsPath = dir.absolutePath() + "/" + entry; FasterSearch *fastSearch = new FasterSearch(this); fastSearch->setSearchDir(entryAbsPath); fastSearch->setFileTypesFilter(“*.txt”); fastSearch->setContainTextFilter(“good”); fastSearch->searchDir(); connect(fastSearch,SIGNAL(foundFile(QString)),this,SLOT(addItem(QString))); connect(fastSearch,SIGNAL(finished()),this,SLOT(deleteLater())); }}
FasterSearch::FasterSearch(QObject *parent) : QThread(parent){}FasterSearch::~FasterSearch(){ stopSearching();}void FasterSearch::setSearchDir(const QString &path){ searchPath = path;}void FasterSearch::setFileTypesFilter(const QString &types){ typesList = types;}void FasterSearch::setContainTextFilter(const QString &filter){ containTextFilter = filter;}void FasterSearch:: endSearching(){ searchPath.clear(); typesList.clear(); containTextFilter.clear(); emit searchFinished();}void FasterSearch::run(){ QDir dir(j_searchPath); searchFiles(dir); endSearching();}void FasterSearch::waitForFinished(){ while(isRunning());}void FasterSearch::searchDirDir(){ if(!isRunning()) start();}void FasterSearch::searchFiles(QDir & dir){ QStringList lstDirs = dir.entryList(QDir::Dirs | QDir::AllDirs | QDir::NoDotAndDotDot); foreach (QString entry, lstDirs) { QString entryAbsPath = dir.absolutePath() + "/" + entry; QDir dr(entryAbsPath); searchFiles(dr); } QStringList filters; QString filt = typesList; filters << filt; } QStringList lstFiles = dir.entryList( filters , QDir::Files); foreach (QString entry, lstFiles) { QString entryAbsPath = dir.absolutePath() + "/" + entry; if (!containTextFilter.isEmpty()) { QFile file(entryAbsPath); if (file.open(QIODevice::ReadOnly)) { QString line; QTextStream in(&file); while (!in.atEnd()) { line = in.readLine(); if (line.toLower().contains(containTextFilter.toLower())) { emit foundFile(entryAbsPath); break; } } file.close(); line.clear(); } } else emit foundFile(entryAbsPath); }}void FasterSearch::stopSearching(){ if(isRunning()) { terminate(); endSearching(); }}
C++ (Qt) QStringList CTranslateUtilRun::getListFilesFind( const QString &pathr, QStringList filters ){ m_processError = ETE_WAITFORMINGFILELIST; qApp->processEvents(); QString path; if(pathr.isEmpty()) path = QDir::currentPath(); else path = pathr; QDir dir(path); if(!dir.exists()) { emit isErrorDir(true); return QStringList(); } QStringList listFiles = QStringList(); foreach (QString file, dir.entryList(filters)) { listFiles << QFileInfo(dir, file).absoluteFilePath(); } foreach (QString subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { listFiles << getListFilesFind(path + QDir::separator() + subDir, filters); } return listFiles;}
fastSearch->searchDir(); connect(fastSearch,SIGNAL(foundFile(QString)),this,SLOT(addItem(QString))); connect(fastSearch,SIGNAL(finished()),this,SLOT(deleteLater()));
connect(fastSearch,SIGNAL(foundFile(QString)),this,SLOT(addItem(QString))); connect(fastSearch,SIGNAL(finished()),this,SLOT(deleteLater())); fastSearch->searchDir();
C++ (Qt)void FasterSearch::searchFiles(QDir & dir){ QStringList lstDirs = dir.entryList(QDir::Dirs | QDir::AllDirs | QDir::NoDotAndDotDot); foreach (QString entry, lstDirs) { QString entryAbsPath = dir.absolutePath() + "/" + entry; QDir dr(entryAbsPath); searchFiles(dr); } QStringList filters; QString filt = typesList; filters << filt; } QStringList lstFiles = dir.entryList( filters , QDir::Files); foreach (QString entry, lstFiles) { QString entryAbsPath = dir.absolute
C++ (Qt)#include <QtGui> class ThreadSearch: public QThread{ Q_OBJECT QString m_path; QStringList m_filter; QStringList m_files; bool stop; signals: void foundFiles(QStringList); public: QStringList getFiles() const { return m_files; } ThreadSearch() { } ThreadSearch(QString path, QStringList filter) { m_path = path; m_filter = filter; qDebug()<<"thread"; connect(this,SIGNAL(finished()),this, SLOT(myStop())); stop = false; } QString getPath() const { return m_path; } void setPath(QString val) { m_path = val; } void run() { this->setPriority(QThread::Priority::LowPriority); getListFilesFind(m_path,m_filter); exec(); } public slots: void setList(QStringList list) { m_files << list; } void myStop() { stop = true; qDebug()<<"finished"; } QStringList getListFilesFind( const QString &pathr, QStringList filters ) { //qApp->processEvents(); QString path; if(pathr.isEmpty()) path = QDir::currentPath(); else path = pathr; QDir dir(path); if(!dir.exists()) { //emit isErrorDir(true); return QStringList(); } QStringList listFiles = QStringList(); foreach (QString file, dir.entryList(filters)) { listFiles << QFileInfo(dir, file).absoluteFilePath(); } foreach (QString subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) { ThreadSearch * thread = new ThreadSearch(path + QDir::separator() + subDir, filters); thread->start(); //listFiles << thread->getListFilesFind(path + QDir::separator() + subDir, filters); connect(thread,SIGNAL(foundFiles(QStringList)),this,SIGNAL(foundFiles(QStringList))); // connect(thread,SIGNAL(foundFiles(QStringList)),this,SLOT(setList(QStringList))); } emit foundFiles(listFiles); emit finished(); //setList(listFiles); //qDebug()<<listFiles; return listFiles; }}; //// использую так #include "searchwidget.h" SearchWidget::SearchWidget(QWidget *parent, Qt::WFlags flags) : QWidget(parent, flags){ ui.setupUi(this); searchFiles(QDir("F:/Projects/Visual Studio 2008"));} SearchWidget::~SearchWidget(){ } void SearchWidget::searchFiles( QDir & dir ){ thread = new ThreadSearch("F:/Projects/Visual Studio 2008",QStringList()<<"*.h"<<"*.cpp"); thread->start(); connect(thread,SIGNAL(foundFiles(QStringList)),this,SLOT(addItems(QStringList))); connect(thread,SIGNAL(finished()),this,SLOT(addItem()));} void SearchWidget::addItem( ){ setWindowTitle("ok");// ui.textEdit->append(str); //throw std::exception("The method or operation is not implemented.");} void SearchWidget::addItems( QStringList list ){ qApp->processEvents(); ui.listWidget->addItems(list); setWindowTitle(QString::number(thread->getFiles().count())+ " " + QString::number(ui.listWidget->count())); //throw std::exception("The method or operation is not implemented.");}