Advance the progress bar in the foreground when a long running task

in the background (e.g. git clone).
This commit is contained in:
2023-08-06 20:44:26 +02:00
parent 1fd2269753
commit 4ff3b0efdf
6 changed files with 98 additions and 22 deletions

View File

@@ -12,7 +12,9 @@ MainWindow::MainWindow(Worker *worker, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_worker(worker)
, m_width(70) {
, m_width(70)
, m_progressRunning(false)
, m_progressValue(0) {
ui->setupUi(this);
ui->updateProgress->setRange(0, 100);
@@ -71,11 +73,51 @@ MainWindow::~MainWindow() {
void MainWindow::customEvent(QEvent *event) {
if (event->type() == ProgressEvent::type()) {
int progress = ((ProgressEvent *)(event))->progressPercent();
if (progress > 0) {
ui->updateProgress->setValue(progress);
ProgressEvent *pevent = (ProgressEvent *)event;
int const progress = pevent->progressPercent();
QObject const *sender = pevent->sender();
if (sender == this) {
switch(progress) {
case 0: {
ui->updateProgress->reset();
} break;
case START_PROGRESS_LOOP: {
m_progressRunning = true;
ui->updateProgress->reset();
m_progressValue = 10;
QApplication::postEvent(this, new ProgressEvent(this, m_progressValue));
} break;
case STOP_PROGRESS_LOOP: {
m_progressRunning = false;
m_progressValue -= 10;
m_worker->setProgress(m_progressValue/10);
} break;
default: {
if (m_progressRunning) {
m_progressValue = progress;
ui->updateProgress->setValue(progress/10);
QApplication::postEvent(this, new ProgressEvent(this, progress+10));
QThread::msleep(100);
}
}
}
} else
if (sender == m_worker) {
switch(progress) {
case 0: {
ui->updateProgress->reset();
} break;
case START_PROGRESS_LOOP: {
QApplication::postEvent(this, new ProgressEvent(this, START_PROGRESS_LOOP));
} break;
case STOP_PROGRESS_LOOP: {
QApplication::postEvent(this, new ProgressEvent(this, STOP_PROGRESS_LOOP));
} break;
default:{
ui->updateProgress->setValue(progress);
}}
} else {
ui->updateProgress->reset();
qCritical() << "!!! UNKNOWN SENDER !!!";
}
}
}