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 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 Offline
      J.HilkJ Offline
      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 Offline
          J.HilkJ Offline
          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 Offline
              J.HilkJ Offline
              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
                  • VineelaV Vineela

                    @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 Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #22

                    @Vineela
                    you're welcome. :)

                    I'm glad it's now working for you!


                    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
                      you're welcome. :)

                      I'm glad it's now working for you!

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

                      @J.Hilk oh yes it is working great ; P

                      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