From dd59c0c54ade7de69a8fda46078a64e7e63a7ad2 Mon Sep 17 00:00:00 2001 From: NeoTheFox Date: Tue, 24 Mar 2015 16:13:43 +0300 Subject: [PATCH] Added warning on closeEvent --- mainwindow.cpp | 28 ++++++++++++++++++++++++++++ mainwindow.h | 1 + 2 files changed, 29 insertions(+) diff --git a/mainwindow.cpp b/mainwindow.cpp index dbe44b4..b017d2e 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -952,6 +952,34 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *event) return QMainWindow::eventFilter(obj, event); } +void MainWindow::closeEvent(QCloseEvent *event) +{ + //If connected to printer - show warning dialog + if(opened) + { + //Create dialog + QMessageBox dialog(this); + + //Set it up - different text if just connected and if printing + if(sending) dialog.setText(tr("Printer is working!\nAre you shure you want to exit?")); + else dialog.setText(tr("Printer is connected!\nAre you shure you want tot exit?")); + dialog.setIcon(QMessageBox::Warning); + + //Save pointer to check what button was clicked + QPushButton *exitButton = dialog.addButton(tr("Exit"), QMessageBox::AcceptRole); + dialog.addButton(QMessageBox::Cancel); + + //Show dialog + dialog.exec(); + + //Process responce + if(dialog.clickedButton() == exitButton) event->accept(); + else event->ignore(); + } + //Close immediately if not connected + else event->ignore(); +} + void MainWindow::recentClicked() { //Actually a dirty hack, but it is fast and simple diff --git a/mainwindow.h b/mainwindow.h index fc3c1da..ebc3df5 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -57,6 +57,7 @@ protected: QStringList userHistory; QSerialPortInfo printerinfo; + void closeEvent(QCloseEvent *event); bool eventFilter(QObject *target, QEvent *event); private: