From 115ec041d3eb38ee93c481c76c3aa8961050fa16 Mon Sep 17 00:00:00 2001 From: h-4nd-h Date: Sun, 20 Sep 2020 10:38:18 +0200 Subject: [PATCH] Add wait dialog, fixed ui crash isuue --- HarwareTest.pro | 9 ++++++--- main.cpp | 10 ++++++++++ mainwindow.cpp | 31 +++++++++++++++++++++++++++--- mainwindow.h | 6 ++++++ waitdialog.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ waitdialog.h | 30 +++++++++++++++++++++++++++++ waitdialog.ui | 37 ++++++++++++++++++++++++++++++++++++ 7 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 waitdialog.cpp create mode 100644 waitdialog.h create mode 100644 waitdialog.ui diff --git a/HarwareTest.pro b/HarwareTest.pro index 06f436e..7cbc43b 100644 --- a/HarwareTest.pro +++ b/HarwareTest.pro @@ -18,14 +18,17 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ mainwindow.cpp \ - /home/hasis/Desktop/pcie/pcie_driver/api/api.cpp + /home/hasis/Desktop/pcie/pcie_driver/api/api.cpp \ + waitdialog.cpp HEADERS += \ mainwindow.h \ - /home/hasis/Desktop/pcie/pcie_driver/api/api.h + /home/hasis/Desktop/pcie/pcie_driver/api/api.h \ + waitdialog.h FORMS += \ - mainwindow.ui + mainwindow.ui \ + waitdialog.ui INCLUDEPATH += /home/hasis/Desktop/pcie/pcie_driver/api/ diff --git a/main.cpp b/main.cpp index fd3e533..c2b691f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,21 @@ #include "mainwindow.h" #include +#include +#include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; + w.setGeometry( + QStyle::alignedRect( + Qt::LeftToRight, + Qt::AlignCenter, + w.size(), + qApp->desktop()->availableGeometry() + ) + ); w.show(); return a.exec(); } diff --git a/mainwindow.cpp b/mainwindow.cpp index 7c9faf5..c7a813d 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -20,6 +20,9 @@ MainWindow::MainWindow(QWidget *parent) _usd = new UltraSoundDevice(); + _dial = new WaitDialog(); + _dial->setModal(true); + this->setFixedSize(this->width(),this->height()); ui->prg_binUpload->setVisible(false); @@ -50,6 +53,8 @@ MainWindow::~MainWindow() delete _settings; delete _usd; + + delete _dial; } /*************************************************************************************************/ @@ -83,7 +88,12 @@ void MainWindow::on_btn_binUpload_clicked() auto path = ui->tb_binFile->text(); - QtConcurrent::run(this, &MainWindow::binaryFileUploader, bar, offset, path); + QFutureWatcher watcher; + connect(&watcher, &QFutureWatcher::finished, this, &MainWindow::threadFinished); + auto future = QtConcurrent::run(this, &MainWindow::binaryFileUploader, bar, offset, path); + watcher.setFuture(future); + _dial->exec(); + disconnect(&watcher, &QFutureWatcher::finished, this, &MainWindow::threadFinished); } /*************************************************************************************************/ @@ -102,7 +112,12 @@ void MainWindow::on_btn_binVerify_clicked() auto path = ui->tb_binFile->text(); - QtConcurrent::run(this, &MainWindow::binaryFileVerifier, bar, offset, path); + QFutureWatcher watcher; + connect(&watcher, &QFutureWatcher::finished, this, &MainWindow::threadFinished); + auto future = QtConcurrent::run(this, &MainWindow::binaryFileVerifier, bar, offset, path); + watcher.setFuture(future); + _dial->exec(); + disconnect(&watcher, &QFutureWatcher::finished, this, &MainWindow::threadFinished); } /*************************************************************************************************/ @@ -425,7 +440,12 @@ void MainWindow::on_btn_fpgaProgram_clicked() auto path = ui->tb_fpgaBit->text(); - QtConcurrent::run(this, &MainWindow::fpgaProgrammer, bar, offset, path); + QFutureWatcher watcher; + connect(&watcher, &QFutureWatcher::finished, this, &MainWindow::threadFinished); + auto future = QtConcurrent::run(this, &MainWindow::fpgaProgrammer, bar, offset, path); + watcher.setFuture(future); + _dial->exec(); + disconnect(&watcher, &QFutureWatcher::finished, this, &MainWindow::threadFinished); } /*************************************************************************************************/ @@ -435,3 +455,8 @@ void MainWindow::newMessage(QString message) msgBox.setText(message); msgBox.exec(); } + +void MainWindow::threadFinished() +{ + _dial->close(); +} diff --git a/mainwindow.h b/mainwindow.h index b224861..635eb29 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,6 +4,8 @@ #include #include +#include "waitdialog.h" + #include "api.h" #define BIN_FILE_PATH "binFilePath" @@ -54,10 +56,14 @@ private slots: void newMessage(QString message); + void threadFinished(); + private: Ui::MainWindow *ui; + WaitDialog* _dial; + QSettings* _settings; UltraSoundDevice* _usd; diff --git a/waitdialog.cpp b/waitdialog.cpp new file mode 100644 index 0000000..fed4c08 --- /dev/null +++ b/waitdialog.cpp @@ -0,0 +1,50 @@ +#include "waitdialog.h" +#include "ui_waitdialog.h" + +#include +#include + +WaitDialog::WaitDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::WaitDialog) +{ + ui->setupUi(this); + + _timer = new QTimer(); + + connect(_timer, &QTimer::timeout, this, &WaitDialog::timeout); + + _timer->start(250); + + setText(); +} + +/*************************************************************************************************/ +WaitDialog::~WaitDialog() +{ + delete ui; + + delete _timer; +} + +/*************************************************************************************************/ +void WaitDialog::timeout() +{ + setText(); +} + +/*************************************************************************************************/ +void WaitDialog::setText() +{ + QString str = "Please wait"; + + for(auto i = 0; i < _state; i++) + str += "."; + + _state++; + + if(_state > 3) + _state = 0; + + ui->l_text->setText(str); +} diff --git a/waitdialog.h b/waitdialog.h new file mode 100644 index 0000000..bfdeca9 --- /dev/null +++ b/waitdialog.h @@ -0,0 +1,30 @@ +#ifndef WAITDIALOG_H +#define WAITDIALOG_H + +#include + +namespace Ui { +class WaitDialog; +} + +class WaitDialog : public QDialog +{ + Q_OBJECT + +public: + explicit WaitDialog(QWidget *parent = nullptr); + ~WaitDialog(); + +private slots: + void timeout(); + +private: + Ui::WaitDialog *ui; + QTimer* _timer; + + int _state; + + void setText(); +}; + +#endif // WAITDIALOG_H diff --git a/waitdialog.ui b/waitdialog.ui new file mode 100644 index 0000000..332911c --- /dev/null +++ b/waitdialog.ui @@ -0,0 +1,37 @@ + + + WaitDialog + + + + 0 + 0 + 219 + 102 + + + + Dialog + + + + + 60 + 40 + 121 + 17 + + + + + 12 + + + + TextLabel + + + + + +