class TLoadProgress : public QDialog{Q_OBJECT // Обяъвления элементов управления пропущены за экономией места в посте QStringList FileNames; int FileIndex; bool ThreadCreated; TLoadThread* Thread;public: explicit TLoadProgress (QStringList& file_names, QWidget *parent = 0); ~TLoadProgress ();public slots: void on_thread_stop (); void on_thread_progress (int value);};TLoadProgress :: TLoadProgress(QStringList& file_names, QWidget *parent) : QDialog(parent), FileNames(file_names), FileIndex(0), ThreadCreated(false){ // Создание элементов управления пропущены за экономией места в посте // Dialog Buttons QHBoxLayout* layoutDialogButtons = new QHBoxLayout(); layoutMain->addLayout(layoutDialogButtons); layoutDialogButtons->addWidget(btnOK = new QPushButton("OK", this)); layoutDialogButtons->addWidget(btnCancel = new QPushButton("Cancel", this)); on_thread_stop();}void TLoadProgress :: on_thread_stop (){ qDebug() << "on thread stop"; if (ThreadCreated) { if (Thread->getExitCode() == 1) { qDebug() << "on thread stop: Error On Exit Code"; } qDebug() << "on thread stop: delete thread"; delete Thread; ThreadCreated = false; }; if (FileIndex < FileNames.count()) { Thread = new TLoadThread(FileNames.at(FileIndex++)); ThreadCreated = true; connect(Thread, SIGNAL(finished()), this, SLOT(on_thread_stop())); connect(Thread, SIGNAL(progress(int)), this, SLOT(on_thread_progress(int))); Thread->start(); } else done(0);}TLoadProgress :: ~TLoadProgress (){ if (ThreadCreated) { qDebug() << "progress destructor: thread delete"; Thread->deleteLater(); }}void TLoadProgress :: on_thread_progress (int value){ lblProgress->setText(QString().setNum(value));}
class TLoadThread : public QThread{ Q_OBJECT QString FileName; QFile File; QTextStream TextStream; int ExitCode; bool StopRequested;public: explicit TLoadThread (QString file_name, QObject *parent = 0); void run (); int getExitCode (); void requestStop (); ~TLoadThread ();signals: void progress (int);public slots:};TLoadThread :: TLoadThread(QString file_name, QObject *parent) : QThread(parent), FileName(file_name), ExitCode(0), StopRequested(false){ File.setFileName(FileName); File.open(QIODevice::ReadOnly); TextStream.setDevice(&File);}void TLoadThread :: run (){ qDebug() << "thread run: Continue Read File From Stream " << FileName; for (double i = 0.0; /*!StopRequested &&*/ i < 100.0; i+=0.001) { emit progress(i); } ExitCode = 0;}int TLoadThread :: getExitCode (){ return ExitCode;}void TLoadThread :: requestStop (){ StopRequested = true;}TLoadThread :: ~TLoadThread (){ qDebug() << "thread destructor: thread destroy"; File.close();}
void TMainWindow :: on_btnFilesOpen_clicked (){ QFileDialog dlg(this, "Open File"); dlg.setFileMode(QFileDialog::ExistingFile); dlg.setFilter("Data Files (*.*)"); dlg.setAcceptMode(QFileDialog::AcceptOpen); dlg.setFileMode(QFileDialog::ExistingFiles); if (dlg.exec()) { QStringList file_names = dlg.selectedFiles(); TLoadProgress* LoadProgress = new TLoadProgress(file_names, this); LoadProgress->exec(); }}