Solved Quiting Qt app by Alert Message
-
-
@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();
-
@J.Hilk well
protected:
void keyPressEvent (QKeyEvent* event)override;
I've added this to my header MainWindow all working awesome
thnk u so much for your kind patience . -
@Vineela
you're welcome. :)I'm glad it's now working for you!
-
@J.Hilk oh yes it is working great ; P