使用QTimer,QThread和Progress Bar

问题描述 投票:0回答:3

我想使用与Ui线程分开的计算。计算方法 - MainWindowМетод的私有插槽。在计算过程中,在ProgressBar Window的对象OutputData - 字段中逐步传输计算数据。执行计算的线程也是主窗口的字段。

主窗口构造函数,创建线程并开始计算连接到线程的开头:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);

calculation = new QThread(this);

settings.setToDefault();
outputData.setToDefault();
calculationDone = false;

connect(calculation, SIGNAL(started()), this, SLOT(calculate()));

ui->results_display->setText("Загрузите <b>параметры</b> и начинайте расчёты!");
}

按钮方法,使用/ Progress Bar启动线程和模态窗口:

void MainWindow::on_calculate_button_clicked() {
ui->results_display->clear();
ui->results_display2->clear();

calculation->start();

///TODO QProgressDialog

ProgressDialog progressDialog(&outputData, this);
progressDialog.setModal(true);
progressDialog.exec();

if (progressDialog.result() == QDialog::Rejected) {
    calculation->terminate();
    QMessageBox::critical(this, "Результат", "Расчёт был остановлен!");
} else {
    if (progressDialog.result() == QDialog::Accepted) {
        calculation->quit();
        QMessageBox::about(this, "Результат", "Готово!");
        }
    }
}

模态窗口构造函数设置ProgressBar的参数,创建计时器并设置计时器和更新方法之间的连接:

ProgressDialog::ProgressDialog(OutputData *outputData, QWidget *parent) :
QDialog(parent),
ui(new Ui::ProgressDialog) {
ui->setupUi(this);

data = outputData;

ui->quantity_label->setText("0");
ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(static_cast<int>(data->outputSettings.aircraftQuantity));

timer = new QTimer(this);
timer->setSingleShot(false);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(50);
}

ProgressBar更新方法:

void ProgressDialog::update() {
unsigned long aircraftsDone = data->results.size() + data->unprocessedAircrafts.size();
ui->progressBar->setValue(static_cast<int>(aircraftsDone));
ui->aircraftQunatityDone_label->setText(QString::number(aircraftsDone));
ui->progressBar->repaint();

if (aircraftsDone == data->outputSettings.aircraftQuantity) {
    accept();
   }
}

目前计算效果很好,但没有绘制或更新进度信息。

c++ qt
3个回答
1
投票

这是因为你的calculate方法没有在新线程中调用。

要使用QThread在新线程中运行任何方法,您必须使用QObject将此方法为公共槽的对象(此对象必须从QThread类继承)移动到QObject::moveToThread实例中,然后将您的方法与QThread::started信号连接。在你的情况下,你只需在calculate中的MainWindow插槽和QThread::started中的calculation信号之间建立连接,所以当你调用calculation->start()然后你在你调用calculate的同一个线程中触发calculation->start()方法。

要解决此问题,您应该为calculate方法创建一个单独的类,然后将此类的对象移动到QThread构造函数中的MainWindow中。

我建议你看一下reference的例子


1
投票

这些是Rhathin's answerreference的示例代码。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QThread>
#include "calculationworker.h"
#include "progressdialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QThread *calculationThread;
    CalculationWorker *calculationWorker;

signals:
    void startCalculation();
    void stopCalculation();

private slots:
    void on_calculate_button_clicked();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    calculationThread = new QThread( this );
    calculationWorker = new CalculationWorker();

    calculationWorker->moveToThread( calculationThread );

    connect( this, SIGNAL(startCalculation()), calculationWorker, SLOT(calculate()) );
    connect( this, SIGNAL(stopCalculation()), calculationWorker, SLOT(stopCalculation()), Qt::DirectConnection );

    calculationThread->start();
}

MainWindow::~MainWindow()
{
    calculationThread->quit();
    calculationThread->wait();
    delete calculationWorker;
    delete ui;
}

void MainWindow::on_calculate_button_clicked()
{
    ProgressDialog progressDialog( this );
    connect( calculationWorker, SIGNAL(progress(int, int)), &progressDialog, SLOT(progress(int, int)) );
    connect( calculationWorker, SIGNAL(completed()), &progressDialog, SLOT(completed()) );

    emit startCalculation();

    progressDialog.setModal( true );
    progressDialog.exec();

    if ( progressDialog.result() == QDialog::Rejected ) {
        emit stopCalculation();
    } else if ( progressDialog.result() == QDialog::Accepted ) {
        // Accepted
    }
}

calculationworker.h

#ifndef CALCULATIONWORKER_H
#define CALCULATIONWORKER_H

#include <QObject>
#include <QThread> // msleep()

class CalculationWorker : public QObject
{
    Q_OBJECT
public:
    explicit CalculationWorker(QObject *parent = nullptr);

signals:
    void progress( int processed, int quantity );
    void stopped();
    void completed();

public slots:
    void calculate();
    void stopCalculation();

private:
    bool m_stopFlag;
};

#endif // CALCULATIONWORKER_H

calculationworker.cpp

#include "calculationworker.h"

CalculationWorker::CalculationWorker(QObject *parent) : QObject(parent)
{

}

void CalculationWorker::calculate()
{
    m_stopFlag = false;

    int quantity = 1000;
    for ( int i = 0; i < quantity; i++ ) {
        if ( m_stopFlag ) {
            emit stopped();
            return;
        }

        // Do calculation here
        QThread::msleep( 10 );

        emit progress( i, quantity );
    }

    emit completed();
}

void CalculationWorker::stopCalculation()
{
    m_stopFlag = true;
}

progressdialog.h

#ifndef PROGRESSDIALOG_H
#define PROGRESSDIALOG_H

#include <QDialog>

namespace Ui {
class ProgressDialog;
}

class ProgressDialog : public QDialog
{
    Q_OBJECT

public:
    explicit ProgressDialog(QWidget *parent = nullptr);
    ~ProgressDialog();

private:
    Ui::ProgressDialog *ui;

public slots:
    void progress( int processed, int quantity );
    void completed();
};

#endif // PROGRESSDIALOG_H

progressdialog.cpp

#include "progressdialog.h"
#include "ui_progressdialog.h"

ProgressDialog::ProgressDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProgressDialog)
{
    ui->setupUi(this);
}

ProgressDialog::~ProgressDialog()
{
    delete ui;
}

void ProgressDialog::progress(int processed, int quantity)
{
    ui->progressBar->setMaximum( quantity );
    ui->progressBar->setValue( processed );
}

void ProgressDialog::completed()
{
    accept();
}

0
投票

这些是另一个版本示例代码,在计算时不会发出进度。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QThread>
#include "calculationworker.h"
#include "progressdialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QThread *calculationThread;
    CalculationWorker *calculationWorker;

signals:
    void startCalculation();

private slots:
    void on_calculate_button_clicked();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    calculationThread = new QThread( this );
    calculationWorker = new CalculationWorker();

    calculationWorker->moveToThread( calculationThread );

    connect( this, SIGNAL(startCalculation()), calculationWorker, SLOT(calculate()) );

    calculationThread->start();
}

MainWindow::~MainWindow()
{
    calculationThread->quit();
    calculationThread->wait();
    delete calculationWorker;
    delete ui;
}

void MainWindow::on_calculate_button_clicked()
{
    ProgressDialog progressDialog( calculationWorker ,this );

    emit startCalculation();

    progressDialog.exec();

    if ( progressDialog.result() == QDialog::Rejected ) {
        // Rejected(Stopped)
    } else if ( progressDialog.result() == QDialog::Accepted ) {
        // Accepted
    }
}

calculationworker.h

#ifndef CALCULATIONWORKER_H
#define CALCULATIONWORKER_H

#include <QObject>
#include <QThread> // msleep()

class CalculationWorker : public QObject
{
    Q_OBJECT
public:
    explicit CalculationWorker(QObject *parent = nullptr);

    int getQuantity();
    int getProcessed();

signals:
    void started();
    void stopped();
    void completed();

public slots:
    void calculate();
    void stopCalculation();

private:
    bool m_stopFlag;
    bool m_stopped;

    int m_processed;
    int m_quantity;
};

#endif // CALCULATIONWORKER_H

calculationworker.cpp

#include "calculationworker.h"

CalculationWorker::CalculationWorker(QObject *parent) : QObject(parent)
{
    m_quantity = 1000;
}

int CalculationWorker::getQuantity()
{
    // Return quantity (data->outputSettings.aircraftQuantity)))
    return m_quantity;
}

int CalculationWorker::getProcessed()
{
    // Return processed (data->results.size() + data->unprocessedAircrafts.size())
    return m_processed;
}

void CalculationWorker::calculate()
{
    m_stopFlag = false;

    emit started();

    // Calculation
    for ( m_processed = 0; m_processed < m_quantity; m_processed++ ) {
        if ( m_stopFlag ) {
            // emit stopped();
            return;
        }

        // Do calculation here
        QThread::msleep( 10 );
    }

    emit completed();
}

void CalculationWorker::stopCalculation()
{
    m_stopFlag = true;
}

progressdialog.h

#ifndef PROGRESSDIALOG_H
#define PROGRESSDIALOG_H

#include <QDialog>
#include <QTimer>
#include "calculationworker.h"

namespace Ui {
class ProgressDialog;
}

class ProgressDialog : public QDialog
{
    Q_OBJECT

public:
    explicit ProgressDialog( CalculationWorker *worker, QWidget *parent = nullptr);
    ~ProgressDialog();

private:
    Ui::ProgressDialog *ui;
    CalculationWorker *m_worker;
    QTimer *m_progressTimer;

public slots:
    void started();
    void update();
};

#endif // PROGRESSDIALOG_H

progressdialog.cpp

#include "progressdialog.h"
#include "ui_progressdialog.h"

ProgressDialog::ProgressDialog( CalculationWorker *worker, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProgressDialog),
    m_worker(worker),
    m_progressTimer(new QTimer(parent))
{
    ui->setupUi(this);

    // Connect started signal from worker
    connect( m_worker, SIGNAL(started()), this, SLOT(started()) );

    // Stop calculation by clicking reject button
    connect( this, SIGNAL(rejected()), m_worker, SLOT(stopCalculation()), Qt::DirectConnection );

    // Connect timeout signal to update
    connect( m_progressTimer, SIGNAL(timeout()), this, SLOT(update()) );

    // Stop the timer when the dialog is closed
    connect( this, SIGNAL(finished(int)), m_progressTimer, SLOT(stop()) );
}

ProgressDialog::~ProgressDialog()
{
    delete ui;
}

void ProgressDialog::started()
{
    ui->progressBar->setMaximum( m_worker->getQuantity() );
    m_progressTimer->start( 50 );
}

void ProgressDialog::update()
{
    ui->progressBar->setValue( m_worker->getProcessed() );

    if ( m_worker->getProcessed() == m_worker->getQuantity() ) {
        accept();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.