#ifndef FIND_WORD_IN_FILE_THREAD_H#define FIND_WORD_IN_FILE_THREAD_H #include <QThread>#include <QString>#include <QtDebug>#include <QMutex> class FindWordInFileThread : public QThread{public: FindWordInFileThread(const std::vector<QString>& filePath, const QString& word, size_t startIndex, size_t numElements, std::vector<QString>& foundFiles, QMutex* mutex); virtual void run(); private: const std::vector<QString>& mFilePath; const QString mWord; size_t mStartIndex; size_t mNumElements; std::vector<QString>& mFoundFiles; QMutex* mMutex;}; #endif // FIND_WORD_IN_FILE_THREAD_H
#include "FindWordInFileThread.h" #include <QFile>#include <QTextStream>#include <QFileInfo>#include <QMutexLocker> FindWordInFileThread::FindWordInFileThread(const std::vector<QString>& filePath, const QString& word, size_t startIndex, size_t numElements, std::vector<QString>& foundFiles, QMutex* mutex) : mFilePath(filePath) , mWord(word) , mStartIndex(startIndex) , mNumElements(numElements) , mFoundFiles(foundFiles) , mMutex(mutex){} void FindWordInFileThread::run(){ for (size_t index = mStartIndex; index < mStartIndex + mNumElements; ++index) { QFile file(mFilePath[index]); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream textStream(&file); while (!textStream.atEnd()) { QString line = textStream.readLine(); QMutexLocker locker(mMutex); if (line.contains(mWord, Qt::CaseInsensitive)) { mFoundFiles.push_back(QFileInfo(mFilePath[index]).fileName()); break; } } file.close(); }}
void FindWordInFiles(const QString& word, const size_t numThreads){ QMutex mutex; QTime time(QTime::currentTime()); std::srand(time.msecsSinceStartOfDay()); std::vector<QThread*> threads; threads.reserve(numThreads); QDirIterator dirIt("D:/11", QDirIterator::Subdirectories); std::vector<QString> paths; while (dirIt.hasNext()) { dirIt.next(); if (QFileInfo(dirIt.filePath()).isFile()) { if (QFileInfo(dirIt.filePath()).suffix() == "txt") paths.push_back(dirIt.filePath()); } } const std::vector<Range> ranges = GenerateRanges(numThreads, paths.size()); std::vector<QString> foundFiles; time.start(); for (size_t index = 0; index < numThreads; ++index) threads.push_back(new FindWordInFileThread(paths, word, ranges[index].mStart, ranges[index].mLength, foundFiles, &mutex)); for (size_t index = 0; index < numThreads; ++index) threads[index]->start(); for (size_t index = 0; index < numThreads; ++index) threads[index]->wait(); const int elapsedMs = time.elapsed(); std::sort(foundFiles.begin(), foundFiles.end()); qDebug() << "Elapsed time(ms): " << elapsedMs; for (size_t index = 0; index < foundFiles.size(); ++index) qDebug() << foundFiles[index]; for (size_t index = 0; index < numThreads; ++index) delete threads[index];} int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); const int idealThreadCount = QThread::idealThreadCount(); qDebug() << "One Thread:"; FindWordInFiles("pavlik", 1); qDebug() << ""; qDebug() << "Two Thread:"; FindWordInFiles("pavlik", 2); qDebug() << ""; qDebug() << "Optimal Thread: "; qDebug() <<"Ideal Thread Count:" << idealThreadCount; FindWordInFiles("pavlik", idealThreadCount); qDebug() << ""; qDebug() << "Twenty Thread: "; FindWordInFiles("pavlik", 20); return app.exec();}
QString line = textStream.readLine(); QMutexLocker locker(mMutex); if (line.contains(mWord, Qt::CaseInsensitive)) { mFoundFiles.push_back(QFileInfo(mFilePath[index]).fileName()); break; } }}
void FindWordInFileThread::run(){ for (size_t index = mStartIndex; index < mStartIndex + mNumElements; ++index) { QFile file(mFilePath[index]); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream textStream(&file); while (!textStream.atEnd()) { QString line = textStream.readLine(); if (line.contains(mWord, Qt::CaseInsensitive)) { QMutexLocker locker(mMutex); mFoundFiles.push_back(QFileInfo(mFilePath[index]).fileName()); break; } } file.close(); }}
C++ (Qt)while(!asleep()) sheep++;
#ifndef FINDWORDINFILETHREAD_H#define FINDWORDINFILETHREAD_H#include <QtWidgets>class FindWordInFileThread : public QWidget{ Q_OBJECTpublic: FindWordInFileThread(QWidget *parent = 0); ~FindWordInFileThread();public slots: void start();private: int mStart;};#endif // FINDWORDINFILETHREAD_H
#include "FindWordInFileThread.h"FindWordInFileThread::FindWordInFileThread(QWidget *parent) : QWidget(parent){ setWindowTitle("Find word in file thread"); QLabel* selectDirectory = new QLabel("Select directory:"); QPushButton* start = new QPushButton("&Start"); connect(start, SIGNAL(clicked()), SLOT(start())); QLineEdit* text = new QLineEdit; QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(selectDirectory); layout->addWidget(start); layout->addWidget(text); setLayout(layout); resize(500, 300);}void FindWordInFileThread::start(){}FindWordInFileThread::~FindWordInFileThread(){}
#include "FindWordInFileThread.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication app(argc, argv); FindWordInFileThread findWordInFileThread; findWordInFileThread.show(); return app.exec();}
QString str = QFileDialog::getExistingDirectory(0, "Directory Dialog", "");
C++ (Qt)CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent){ /* нужные указатели, такие как toolButtonSelectDir, * pushButtonStart, lineEditDirName и listWidgetResult * можно (нужно) сдеалать переменными класса */ QVBoxLayout *verticalLayout2; QHBoxLayout *horizontalLayout2; QGroupBox *groupBox; QHBoxLayout *horizontalLayout; QLineEdit *lineEditDirName; QToolButton *toolButtonSelectDir; QPushButton *pushButtonStart; QGroupBox *groupBox2; QVBoxLayout *verticalLayout; QListWidget *listWidgetResult; verticalLayout2 = new QVBoxLayout(this); verticalLayout2->setSpacing(6); verticalLayout2->setContentsMargins(11, 11, 11, 11); horizontalLayout2 = new QHBoxLayout(); horizontalLayout2->setSpacing(6); groupBox = new QGroupBox(this); horizontalLayout = new QHBoxLayout(groupBox); horizontalLayout->setSpacing(6); horizontalLayout->setContentsMargins(11, 11, 11, 11); lineEditDirName = new QLineEdit(groupBox); horizontalLayout->addWidget(lineEditDirName); toolButtonSelectDir = new QToolButton(groupBox); horizontalLayout->addWidget(toolButtonSelectDir); horizontalLayout2->addWidget(groupBox); pushButtonStart = new QPushButton(this); horizontalLayout2->addWidget(pushButtonStart); verticalLayout2->addLayout(horizontalLayout2); groupBox2 = new QGroupBox(this); verticalLayout = new QVBoxLayout(groupBox2); verticalLayout->setSpacing(6); verticalLayout->setContentsMargins(11, 11, 11, 11); listWidgetResult = new QListWidget(groupBox2); verticalLayout->addWidget(listWidgetResult); verticalLayout2->addWidget(groupBox2); setWindowTitle(tr("Поиск файла")); toolButtonSelectDir->setText("..."); pushButtonStart->setText(tr("Начать")); lineEditDirName->setText(tr("d:/some_dir")); resize(600, 600);}
QGroupBox* groupBoxFirst = new QGroupBox("&Select directory:"); QGroupBox* groupBoxSecond = new QGroupBox("&Result found:");