Solved Quiting Qt app by Alert Message
-
When i press back button in my android device the Qt app dies, so my request is when i click the back button I need an alert message which says "Do you wanna close the app", so that later I can operate my app further. Help me out with this, anyone out there??
Thanks in advance.
-
@Vineela
overrride the virtual functionkeyPressEvent
and look for Qt::Key_Backvoid QWidget::keyPressEvent (QKeyEvent* event) { if (event->key () == Qt::Key_Back) { int ret = QMessageBox::warning(this, tr("My Application"), tr("About to close the app.\n" "Do you want to continue?"), QMessageBox::Ok | QMessageBox::Cancel); if(ret == QMessageBox::Ok) qApp->quit(); return; } else { //if not back key, call the baseclass implementation QWidget::keyPressEvent(event); } }
-
@J.Hilk Thanks it worked but the thing is after I clicked back button the alert dialog popped then after 2 seconds the app closed so , what am i suppose to do??
-
Hi,
IIRC, you also need to callevent->accept();
in case the user didn't want to quit.[edit SGaist]: Not needed as @J-Hilk explained below.
-
@SGaist And one more thing when i added the above code to my project,then i enter value to the lineEdit and clicked ok on the keyboard there the project closes.
-
-
@J-Hilk correct, I've mixed that with the QCloseEvent !
-
Fine this is my code,
void QWidget::keyPressEvent (QKeyEvent* event) { if (event->key () == Qt::Key_Back) { int ret = QMessageBox::warning(this, tr("My Application"), tr("About to close the app.\n" "Do you want to continue?"), QMessageBox::Ok | QMessageBox::Cancel); if(ret == QMessageBox::Ok) event->accept(); qApp->quit(); return; } else { //if not back key, call the baseclass implementation QWidget::keyPressEvent(event); } }
So tell me what are the changes to be done im newbie sorry if im wrong guide me.
-
@Vineela said in Quiting Qt app by Alert Message:
if(ret == QMessageBox::Ok)
event->accept();
qApp->quit();setting parenthesis will help ;-)
if(ret == QMessageBox::Ok) { event->accept(); qApp->quit(); }
-
Please use brackets for your if statements. Currently you are quitting your application in any case since only the
event->accept
line is considered in the if resolution. -
Thanks ,yes im getting the alert dialog box when i click back but again within few seconds the app closes and the other thing is the OK button of the keyboard makes the project close when clicked.
-
the code you posted is basically a copy paste version of what I wrote, but I'm pretty sure your class, where you overwrite is only derived from QWidget, you should adapt that to your special case.
Also did you mark it as override in the header file?
I would also add a qDebug statement to see what key event-key is actually detected when you press enter, and the app closes.
-
This post is deleted! -
Well yes i've marked now and by the way i've added qDebug to check what happens ,well this is what happened ..
void QWidget::keyPressEvent (QKeyEvent* event) { if (event->key () == Qt::Key_Back) { qDebug()<<"11111"; int ret = QMessageBox::warning(this, tr("My Application"), tr("About to close the app.\n" "Do you want to continue?"), QMessageBox::Ok | QMessageBox::Cancel); qDebug()<<"222222"; if(ret == QMessageBox::Ok){ event->accept(); qDebug()<<"333333"; qApp->quit(); qDebug()<<"444444"; } return; } else { //if not back key, call the baseclass implementation QWidget::keyPressEvent(event); qDebug()<<"555555"; } }
Here when the app closes it just displays qDebug 111111 and 22222 from there the projects closes automatically and nothing prints except your project is dead.
-
@Vineela strange, mabye the exec of QMessageBox creates unexpected behaviour in the slot.
try
void QWidget::keyPressEvent (QKeyEvent* event) { if (event->key () == Qt::Key_Back) { qDebug()<<"Do nothing for now"; return; } else { //if not back key, call the baseclass implementation QWidget::keyPressEvent(event); } }
If that works, you can change it to this:
void QWidget::keyPressEvent (QKeyEvent* event) { if (event->key () == Qt::Key_Back) { qDebug()<<"Exit the function first"; QMetaObject::invokeMethod(this, [=]()->void{ int ret = QMessageBox::warning(this, tr("My Application"), tr("About to close the app.\n" "Do you want to continue?"), QMessageBox::Ok | QMessageBox::Cancel); if(ret == QMessageBox::Ok){ qApp->quit(); } }, Qt::QueueConnection); return; } else { //if not back key, call the baseclass implementation QWidget::keyPressEvent(event); } }
-
@J.Hilk well it is Qt::QueuedConnection not Qt::QueueConnection and yes it worked then thank you so much :D but when i enter some value in line edit and click ok in android keyboard the project closes. what can i do for this now? This Key event is reacting for the Keyboard event too.
-
@Vineela said in Quiting Qt app by Alert Message:
@J.Hilk well it is Qt::QueuedConnection not Qt::QueueConnection and yes it worked then thank you so much :D
Sorry my bad, I usually rely on auto completion and the text editor of the forum doesn't have that :P
but when i enter some value in line edit and click ok in android keyboard the project closes. what can i do for this now? This Key event is reacting for the Keyboard event too.
I still think that is because you don't override the keyevent properly.
At this point I'm just speculating as you havent show your actual code/class jet.
I assume your main.cpp looks like this, or similair enoughint main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
That mean your top most widget is of the class MainWindow that has the base class of QMainWindow.
The overwritten keyPressEvent would than look like this:
void MainWindow::keyPressEvent (QKeyEvent* event) { if (event->key () == Qt::Key_Back) { qDebug()<<"Exit the function first"; QMetaObject::invokeMethod(this, [=]()->void{ int ret = QMessageBox::warning(this, tr("My Application"), tr("About to close the app.\n" "Do you want to continue?"), QMessageBox::Ok | QMessageBox::Cancel); if(ret == QMessageBox::Ok){ qApp->quit(); } }, Qt::QueuedConnection); return; } else { //if not back key, call the baseclass implementation QMainWindow::keyPressEvent(event); } }
That's the setup in one of my projects and its only detecting the hardware back button.
If that's not it, you can allways call
QWidget *QWidget::focusWidget()
, docu,use qobject_cast to cast the returned object/widget to QLineEdit. If the cast in not a nullptr, than do not enter the exit request.
-
@J.Hilk yes your correct its MainWindow but when i replaced it I got an Issue which is
error: no 'void MainWindow::keyPressEvent(QKeyEvent*)' member function declared in class 'MainWindow'
void MainWindow::keyPressEvent (QKeyEvent* event) { -
@Vineela what does your MainWindow header file look like ?
especially the keyPressEvent function declaration, I would expect something like this
protected: void keyPressEvent (QKeyEvent* event)override;
?
-
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); int flag; ~MainWindow();