1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| class Worker : public QObject { Q_OBJECT public slots: void process() { emit progressIni(100); for (int i = 0; i <= 100; ++i) { QThread::sleep(1); emit progressUpdated(i); } emit finished(); }
signals: void progressIni(int value); void progressUpdated(int value); void finished(); };
class QMainWindow : public QWidget { Q_OBJECT
public: QMainWindow(QWidget *parent = nullptr);{} ~QMainWindow();{} private slots: void slotOK();
void updateProgress(int value); void setProgressMaximum(int value); void handleFinished();
private: Ui::QMainWindow*m_pAppUi;
QThread* m_thread; Worker* m_processor; }
void QMainWindow::slotOK() { m_processor = new Worker(); m_thread = new QThread();
connect(m_thread, &QThread::started, m_processor, &Worker::process); connect(m_processor, &Worker::progress, this, &QMainWindow::updateProgress); connect(m_processor, &Worker::processfinished, this, &QMainWindow::handleFinished); connect(m_processor, &Worker::progressIni, this, &QMainWindow::setProgressMaximum); connect(m_thread, &QThread::finished, m_processor, &QObject::deleteLater); connect(m_thread, &QThread::finished, m_thread, &QObject::deleteLater); m_thread->start(); }
void QMainWindow::handleFinished() { if (m_thread) { m_thread->quit(); m_thread->wait(); m_processor = nullptr; m_thread = nullptr; } QMessageBox::information(this, "系统提示", "执行完成"); }
void GeoEntityCrackDlg::setProgressMaximum(int value) { m_pAppUi->progressBar1->setMaximum(value); m_pAppUi->progressBar1->reset(); } void GeoEntityCrackDlg::updateProgress(int value) { m_pAppUi->progressBar1->setValue(value); }
|