Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Quiting Qt app by Alert Message
Forum Updated to NodeBB v4.3 + New Features

Quiting Qt app by Alert Message

Scheduled Pinned Locked Moved Solved Mobile and Embedded
23 Posts 3 Posters 4.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VineelaV Vineela

    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.

    J.HilkJ Online
    J.HilkJ Online
    J.Hilk
    Moderators
    wrote on last edited by J.Hilk
    #2

    @Vineela
    overrride the virtual function keyPressEvent and look for Qt::Key_Back

    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)
                 qApp->quit();
              return;
         } else { 
         //if not back key, call the baseclass implementation
            QWidget::keyPressEvent(event);
        }
    }
    

    http://doc.qt.io/qt-5/qwidget.html#keyPressEvent


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    VineelaV 1 Reply Last reply
    3
    • J.HilkJ J.Hilk

      @Vineela
      overrride the virtual function keyPressEvent and look for Qt::Key_Back

      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)
                   qApp->quit();
                return;
           } else { 
           //if not back key, call the baseclass implementation
              QWidget::keyPressEvent(event);
          }
      }
      

      http://doc.qt.io/qt-5/qwidget.html#keyPressEvent

      VineelaV Offline
      VineelaV Offline
      Vineela
      wrote on last edited by
      #3

      @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??

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #4

        Hi,

        IIRC, you also need to call event->accept(); in case the user didn't want to quit.

        [edit SGaist]: Not needed as @J-Hilk explained below.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        VineelaV J.HilkJ 2 Replies Last reply
        3
        • SGaistS SGaist

          Hi,

          IIRC, you also need to call event->accept(); in case the user didn't want to quit.

          [edit SGaist]: Not needed as @J-Hilk explained below.

          VineelaV Offline
          VineelaV Offline
          Vineela
          wrote on last edited by
          #5

          @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.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            IIRC, you also need to call event->accept(); in case the user didn't want to quit.

            [edit SGaist]: Not needed as @J-Hilk explained below.

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @SGaist
            actually:

            Note that QKeyEvent starts with isAccepted() == true, so you do not need to call QKeyEvent::accept() - just do not call the base class implementation if you act upon the key.

            @Vineela
            can you post your actuall code?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @J-Hilk correct, I've mixed that with the QCloseEvent !

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • VineelaV Offline
                VineelaV Offline
                Vineela
                wrote on last edited by Vineela
                #8

                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.

                J.HilkJ 1 Reply Last reply
                0
                • VineelaV Vineela

                  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.

                  J.HilkJ Online
                  J.HilkJ Online
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #9

                  @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();
                  }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  3
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    3
                    • VineelaV Offline
                      VineelaV Offline
                      Vineela
                      wrote on last edited by
                      #11

                      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.

                      J.HilkJ 1 Reply Last reply
                      0
                      • VineelaV Vineela

                        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.

                        J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by J.Hilk
                        #12

                        @Vineela

                        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.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        0
                        • VineelaV Offline
                          VineelaV Offline
                          Vineela
                          wrote on last edited by
                          #13
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • VineelaV Offline
                            VineelaV Offline
                            Vineela
                            wrote on last edited by
                            #14

                            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.

                            J.HilkJ 1 Reply Last reply
                            0
                            • VineelaV Vineela

                              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.

                              J.HilkJ Online
                              J.HilkJ Online
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #15

                              @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);
                                  }
                              }
                              

                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              VineelaV 1 Reply Last reply
                              0
                              • J.HilkJ J.Hilk

                                @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);
                                    }
                                }
                                
                                VineelaV Offline
                                VineelaV Offline
                                Vineela
                                wrote on last edited by Vineela
                                #16

                                @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.

                                J.HilkJ 1 Reply Last reply
                                0
                                • VineelaV Vineela

                                  @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.

                                  J.HilkJ Online
                                  J.HilkJ Online
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by
                                  #17

                                  @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 enough

                                  int 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.


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  VineelaV 1 Reply Last reply
                                  1
                                  • J.HilkJ J.Hilk

                                    @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 enough

                                    int 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.

                                    VineelaV Offline
                                    VineelaV Offline
                                    Vineela
                                    wrote on last edited by Vineela
                                    #18

                                    @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) {

                                    J.HilkJ 1 Reply Last reply
                                    0
                                    • VineelaV Vineela

                                      @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) {

                                      J.HilkJ Online
                                      J.HilkJ Online
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #19

                                      @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;
                                      

                                      ?


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      VineelaV 2 Replies Last reply
                                      1
                                      • J.HilkJ J.Hilk

                                        @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;
                                        

                                        ?

                                        VineelaV Offline
                                        VineelaV Offline
                                        Vineela
                                        wrote on last edited by
                                        #20

                                        @J.Hilk

                                        
                                        namespace Ui {
                                        class MainWindow;
                                        }
                                        
                                        class MainWindow : public QMainWindow
                                        {
                                        
                                        
                                            Q_OBJECT
                                        
                                        public:
                                            explicit MainWindow(QWidget *parent = 0);
                                            int flag;
                                        
                                            ~MainWindow();
                                        
                                        
                                        1 Reply Last reply
                                        1
                                        • J.HilkJ J.Hilk

                                          @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;
                                          

                                          ?

                                          VineelaV Offline
                                          VineelaV Offline
                                          Vineela
                                          wrote on last edited by
                                          #21

                                          @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 .

                                          J.HilkJ 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved