From bb00f3e9cf820702b4afd6b747ccba9e5edd2c2e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 6 Sep 2018 17:28:11 +0200 Subject: Add own timeout dialog with two buttons --- src/timeoutdialog.cpp | 42 +++++++++++----------- src/timeoutdialog.h | 13 ++++--- src/timeoutdialog.ui | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 26 deletions(-) create mode 100644 src/timeoutdialog.ui diff --git a/src/timeoutdialog.cpp b/src/timeoutdialog.cpp index f9787fb..6f12ca2 100644 --- a/src/timeoutdialog.cpp +++ b/src/timeoutdialog.cpp @@ -2,47 +2,47 @@ // Author: Manuel Schneider #include "timeoutdialog.h" +#include "ui_timeoutdialog.h" #include #include +#include TimeOutDialog::TimeOutDialog(int time, QWidget *parent) - : QProgressDialog(parent), _time(time) + : QDialog(parent), _ui(new Ui::TimeOutDialog), _time(time) { - QObject::connect(&_timer, SIGNAL(timeout()), this, SLOT(update())); - QObject::connect(this, SIGNAL(canceled()), this, SLOT(cancel())); - + _ui->setupUi(this); + QObject::connect(&_timer, &QTimer::timeout, this, &TimeOutDialog::update); + QObject::connect(_ui->buttonBox, &QDialogButtonBox::clicked, [this](QAbstractButton *button) { + _timer.stop(); + if (button == _ui->buttonBox->button(QDialogButtonBox::Discard)) { + _time = 0; + } + }); + QObject::connect(_ui->buttonBox, &QDialogButtonBox::rejected, [this]() { + _timer.stop(); + _time = 0; // So wasCanceled() returns true + }); // QProgressDialog takes ownership of QProgressBar - QProgressBar *qbar = new QProgressBar(this); - qbar->setFormat(trUtf8("%v seconds")); - qbar->setMaximum(_time); - qbar->setMinimum(0); - qbar->setValue(_time); - setBar(qbar); + _ui->progressBar->setMaximum(_time); + _ui->progressBar->setValue(_time); + setWindowTitle(" "); + setWindowFlag(Qt::WindowStaysOnTopHint, true); _timer.start(1000); } void TimeOutDialog::hideEvent(QHideEvent *e) { - QProgressDialog::hideEvent(e); + QDialog::hideEvent(e); _timer.stop(); } -//___________________________________________________________________________ void TimeOutDialog::update() { --_time; if (_time == 0) { _timer.stop(); - accept(); } else { - setValue(_time); + _ui->progressBar->setValue(_time); } } - - -//___________________________________________________________________________ -void TimeOutDialog::cancel() -{ - _timer.stop(); -} diff --git a/src/timeoutdialog.h b/src/timeoutdialog.h index eb853f3..b70f753 100644 --- a/src/timeoutdialog.h +++ b/src/timeoutdialog.h @@ -7,24 +7,27 @@ #include #include -class TimeOutDialog : public QProgressDialog +namespace Ui { +class TimeOutDialog; +} + +class TimeOutDialog : public QDialog { Q_OBJECT public: - TimeOutDialog(int time, QWidget *parent = 0); + TimeOutDialog(int time, QWidget *parent = nullptr); bool isActive() const { return _timer.isActive(); } + bool wasCanceled() const { return _time > 0; } private: + Ui::TimeOutDialog *_ui; int _time; QTimer _timer; protected: void hideEvent(QHideEvent *event) override; -public slots: - void cancel(); - private slots: void update(); }; diff --git a/src/timeoutdialog.ui b/src/timeoutdialog.ui new file mode 100644 index 0000000..96160bc --- /dev/null +++ b/src/timeoutdialog.ui @@ -0,0 +1,97 @@ + + + TimeOutDialog + + + + 0 + 0 + 395 + 114 + + + + Dialog + + + + + + Screen configuration changed. Do you want to keep the new settings? + + + true + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + 24 + + + %v seconds + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Apply|QDialogButtonBox::Discard + + + + + + + + + buttonBox + accepted() + TimeOutDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + TimeOutDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + -- cgit v1.2.3-55-g7522