跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  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

已排程 已置頂 已鎖定 已移動 Solved Mobile and Embedded
23 貼文 3 Posters 4.0k 瀏覽 2 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • VineelaV 離線
    VineelaV 離線
    Vineela
    寫於 最後由 編輯
    #1

    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 1 條回覆 最後回覆
    0
    • 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 離線
      J.HilkJ 離線
      J.Hilk
      Moderators
      寫於 最後由 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 條回覆 最後回覆
      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 離線
        VineelaV 離線
        Vineela
        寫於 最後由 編輯
        #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 條回覆 最後回覆
        0
        • SGaistS 離線
          SGaistS 離線
          SGaist
          Lifetime Qt Champion
          寫於 最後由 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 條回覆 最後回覆
          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 離線
            VineelaV 離線
            Vineela
            寫於 最後由 編輯
            #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 條回覆 最後回覆
            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 離線
              J.HilkJ 離線
              J.Hilk
              Moderators
              寫於 最後由 編輯
              #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 條回覆 最後回覆
              2
              • SGaistS 離線
                SGaistS 離線
                SGaist
                Lifetime Qt Champion
                寫於 最後由 編輯
                #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 條回覆 最後回覆
                0
                • VineelaV 離線
                  VineelaV 離線
                  Vineela
                  寫於 最後由 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 條回覆 最後回覆
                  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 離線
                    J.HilkJ 離線
                    J.Hilk
                    Moderators
                    寫於 最後由 編輯
                    #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 條回覆 最後回覆
                    3
                    • SGaistS 離線
                      SGaistS 離線
                      SGaist
                      Lifetime Qt Champion
                      寫於 最後由 編輯
                      #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 條回覆 最後回覆
                      3
                      • VineelaV 離線
                        VineelaV 離線
                        Vineela
                        寫於 最後由 編輯
                        #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 條回覆 最後回覆
                        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 離線
                          J.HilkJ 離線
                          J.Hilk
                          Moderators
                          寫於 最後由 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 條回覆 最後回覆
                          0
                          • VineelaV 離線
                            VineelaV 離線
                            Vineela
                            寫於 最後由 編輯
                            #13
                            此回覆已被刪除!
                            1 條回覆 最後回覆
                            0
                            • VineelaV 離線
                              VineelaV 離線
                              Vineela
                              寫於 最後由 編輯
                              #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 條回覆 最後回覆
                              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 離線
                                J.HilkJ 離線
                                J.Hilk
                                Moderators
                                寫於 最後由 編輯
                                #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 條回覆 最後回覆
                                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 離線
                                  VineelaV 離線
                                  Vineela
                                  寫於 最後由 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 條回覆 最後回覆
                                  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 離線
                                    J.HilkJ 離線
                                    J.Hilk
                                    Moderators
                                    寫於 最後由 編輯
                                    #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 條回覆 最後回覆
                                    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 離線
                                      VineelaV 離線
                                      Vineela
                                      寫於 最後由 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 條回覆 最後回覆
                                      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 離線
                                        J.HilkJ 離線
                                        J.Hilk
                                        Moderators
                                        寫於 最後由 編輯
                                        #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 條回覆 最後回覆
                                        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 離線
                                          VineelaV 離線
                                          Vineela
                                          寫於 最後由 編輯
                                          #20

                                          @J.Hilk

                                          
                                          namespace Ui {
                                          class MainWindow;
                                          }
                                          
                                          class MainWindow : public QMainWindow
                                          {
                                          
                                          
                                              Q_OBJECT
                                          
                                          public:
                                              explicit MainWindow(QWidget *parent = 0);
                                              int flag;
                                          
                                              ~MainWindow();
                                          
                                          
                                          1 條回覆 最後回覆
                                          1

                                          • 登入

                                          • Login or register to search.
                                          • 第一個貼文
                                            最後的貼文
                                          0
                                          • 版面
                                          • 最新
                                          • 標籤
                                          • 熱門
                                          • 使用者
                                          • 群組
                                          • 搜尋
                                          • Get Qt Extensions
                                          • Unsolved