Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to disable ESC key "close" the QProgressDialog ?
QtWS25 Last Chance

how to disable ESC key "close" the QProgressDialog ?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 10.3k Views
  • 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.
  • joeQJ joeQ

    I use the QProgressDialog in my project, but i find, when i pressed the esc key, the QProgressDialog close. i just want to click the cancelButton and then the dialog close.

    so, i subclass QProgressDialog, and reimplement the closeEvent, and keyPressEvent. i can ignore the closeEvent. But, the esc key also can close the dialog.

    i read some reference. they said use the eventFilter in mainwindow, but i try it. not work.

    help me. thank u very much.

    in the eventFilter, i find, when i press the esc key, the event type is "hide -> hideToParent". not the close.

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

    @joeQ
    do you call event->accept() and event->ignore() in your keyPress event?

    void keyPressEvent(QKeyEvent *event){
        if(event->key() == Qt::Key_Escape){
            event->accept();
        }else 
           event->ignore();
    }
    

    iirc, this should not pass the EsC-Key event to the QProgressDialog.


    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.

    joeQJ 2 Replies Last reply
    0
    • J.HilkJ J.Hilk

      @joeQ
      do you call event->accept() and event->ignore() in your keyPress event?

      void keyPressEvent(QKeyEvent *event){
          if(event->key() == Qt::Key_Escape){
              event->accept();
          }else 
             event->ignore();
      }
      

      iirc, this should not pass the EsC-Key event to the QProgressDialog.

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #3

      @J.Hilk i try it, but not work.

      bool Dialog::eventFilter(QObject *obj, QEvent *event)
      {
          if(event->type() == QEvent::Hide || event->type() == QEvent::HideToParent){
              qDebug() << obj << "--" << event->type();
              event->accept();
              return true;
          }else if(event->type() == QEvent::KeyPress) {
              QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
              qDebug() << "Ate key press" << keyEvent->key();
              event->accept();
              return true;
          } else {
              // pass the event on to the parent class
              return QDialog::eventFilter(obj, event);
          }
      }
      
      void Dialog::on_btn_clicked()
      {
          int numFiles = 100;
          ProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
          progress.installEventFilter(this);
          progress.setWindowModality(Qt::WindowModal);
      
          for (int i = 0; i < numFiles; i++) {
              progress.setValue(i);
              QThread::msleep(100);
              if (progress.wasCanceled())
                  break;
              //... copy one file
          }
          progress.setValue(numFiles);
      }
      
      

      Just do it!

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

        @joeQ
        do you call event->accept() and event->ignore() in your keyPress event?

        void keyPressEvent(QKeyEvent *event){
            if(event->key() == Qt::Key_Escape){
                event->accept();
            }else 
               event->ignore();
        }
        

        iirc, this should not pass the EsC-Key event to the QProgressDialog.

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #4

        @J.Hilk i also tried it, also not work. ProgressDialog is subclass of QProgressDialog

        void ProgressDialog::closeEvent(QCloseEvent *event)
        {
            event->ignore(); // work
        }
        
        void ProgressDialog::keyPressEvent(QKeyEvent *event)
        {
            if(event->key() == Qt::Key_Escape){
                event->ignore(); // not work
            }else{
                QProgressDialog::keyPressEvent(event);
            }
        }
        
        void ProgressDialog::hideEvent(QHideEvent *event)
        {
            event->ignore(); // not work
        }
        
        

        Just do it!

        J.HilkJ 1 Reply Last reply
        0
        • joeQJ joeQ

          @J.Hilk i also tried it, also not work. ProgressDialog is subclass of QProgressDialog

          void ProgressDialog::closeEvent(QCloseEvent *event)
          {
              event->ignore(); // work
          }
          
          void ProgressDialog::keyPressEvent(QKeyEvent *event)
          {
              if(event->key() == Qt::Key_Escape){
                  event->ignore(); // not work
              }else{
                  QProgressDialog::keyPressEvent(event);
              }
          }
          
          void ProgressDialog::hideEvent(QHideEvent *event)
          {
              event->ignore(); // not work
          }
          
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #5

          @joeQ said in how to disable ESC key "close" the QProgressDialog ?:

          void ProgressDialog::keyPressEvent(QKeyEvent *event)
          {
          if(event->key() == Qt::Key_Escape){
          event->ignore(); // not work
          }else{
          QProgressDialog::keyPressEvent(event);
          }
          }

          That want work, event->ignore() means, that the event is passed on same as QProgressDialog::keyPressEvent(event); You'll have to accept the event and do nothing with it.
          eg:

           void ProgressDialog::keyPressEvent(QKeyEvent *event)
           {
               if(event->key() == Qt::Key_Escape){
                   event->accept(); 
                   return;
               }else{
                   QProgressDialog::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.

          joeQJ 3 Replies Last reply
          2
          • J.HilkJ J.Hilk

            @joeQ said in how to disable ESC key "close" the QProgressDialog ?:

            void ProgressDialog::keyPressEvent(QKeyEvent *event)
            {
            if(event->key() == Qt::Key_Escape){
            event->ignore(); // not work
            }else{
            QProgressDialog::keyPressEvent(event);
            }
            }

            That want work, event->ignore() means, that the event is passed on same as QProgressDialog::keyPressEvent(event); You'll have to accept the event and do nothing with it.
            eg:

             void ProgressDialog::keyPressEvent(QKeyEvent *event)
             {
                 if(event->key() == Qt::Key_Escape){
                     event->accept(); 
                     return;
                 }else{
                     QProgressDialog::keyPressEvent(event);
                 }
             }
            
            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #6

            @J.Hilk The key is I cann't capture the ESC press event in keyPressEvent function, ProgressDialog is subclass of QProgressDialog. like it;

            void ProgressDialog::keyPressEvent(QKeyEvent *event)
            {
                if(event->key() == Qt::Key_Escape){
                    qDebug("Esc press!"); // when i pressed the esc key , there was nothing print...
                }else{
                    QProgressDialog::keyPressEvent(event);
                }
            
                return;
            }
            

            When i pressed the esc key, what event happend?

            Just do it!

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

              @joeQ said in how to disable ESC key "close" the QProgressDialog ?:

              void ProgressDialog::keyPressEvent(QKeyEvent *event)
              {
              if(event->key() == Qt::Key_Escape){
              event->ignore(); // not work
              }else{
              QProgressDialog::keyPressEvent(event);
              }
              }

              That want work, event->ignore() means, that the event is passed on same as QProgressDialog::keyPressEvent(event); You'll have to accept the event and do nothing with it.
              eg:

               void ProgressDialog::keyPressEvent(QKeyEvent *event)
               {
                   if(event->key() == Qt::Key_Escape){
                       event->accept(); 
                       return;
                   }else{
                       QProgressDialog::keyPressEvent(event);
                   }
               }
              
              joeQJ Offline
              joeQJ Offline
              joeQ
              wrote on last edited by
              #7

              @J.Hilk I tried others key. I can capture all the key press event , except the esc press event;

              Just do it!

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

                @joeQ said in how to disable ESC key "close" the QProgressDialog ?:

                void ProgressDialog::keyPressEvent(QKeyEvent *event)
                {
                if(event->key() == Qt::Key_Escape){
                event->ignore(); // not work
                }else{
                QProgressDialog::keyPressEvent(event);
                }
                }

                That want work, event->ignore() means, that the event is passed on same as QProgressDialog::keyPressEvent(event); You'll have to accept the event and do nothing with it.
                eg:

                 void ProgressDialog::keyPressEvent(QKeyEvent *event)
                 {
                     if(event->key() == Qt::Key_Escape){
                         event->accept(); 
                         return;
                     }else{
                         QProgressDialog::keyPressEvent(event);
                     }
                 }
                
                joeQJ Offline
                joeQJ Offline
                joeQ
                wrote on last edited by
                #8

                @J.Hilk I also tried the event fliter.

                bool Dialog::eventFilter(QObject *obj, QEvent *event)
                {
                    qDebug() << obj << "====" << event->type(); // I want to know, what event passed when i pressed the esc key.
                    if(event->type() == QEvent::KeyPress){
                        qDebug("Key press");
                        return true;
                    }else{
                        // pass the event on to the parent class
                        return QDialog::eventFilter(obj, event);
                    }
                }
                
                void Dialog::on_btn_clicked()
                {
                    int numFiles = 100;
                    ProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
                    progress.installEventFilter(this);
                    progress.setWindowModality(Qt::WindowModal);
                
                    for (int i = 0; i < numFiles; i++) {
                        progress.setValue(i);
                        QThread::msleep(100);
                        if (progress.wasCanceled())
                            break;
                        //... copy one file
                    }
                    progress.setValue(numFiles);
                }
                
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                ...
                ...
                QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(ShortcutOverride) // at here, i pressed the esc key.
                QProgressDialog(0x28c5d4) ==== QEvent::Type(WindowDeactivate)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(ActivationChange)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Hide)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(HideToParent)
                QProgressDialog(0x28c5d4) ==== QEvent::Type(Leave)
                QWidget(0x28c5d4) ==== QEvent::Type(PlatformSurface)
                QWidget(0x28c5d4) ==== QEvent::Type(WinIdChange)
                QWidget(0x28c5d4) ==== QEvent::Type(Destroy)
                

                I don't want let the esc key to close the QProgressdialog, How to do ?

                Just do it!

                J.HilkJ 1 Reply Last reply
                0
                • joeQJ joeQ

                  @J.Hilk I also tried the event fliter.

                  bool Dialog::eventFilter(QObject *obj, QEvent *event)
                  {
                      qDebug() << obj << "====" << event->type(); // I want to know, what event passed when i pressed the esc key.
                      if(event->type() == QEvent::KeyPress){
                          qDebug("Key press");
                          return true;
                      }else{
                          // pass the event on to the parent class
                          return QDialog::eventFilter(obj, event);
                      }
                  }
                  
                  void Dialog::on_btn_clicked()
                  {
                      int numFiles = 100;
                      ProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
                      progress.installEventFilter(this);
                      progress.setWindowModality(Qt::WindowModal);
                  
                      for (int i = 0; i < numFiles; i++) {
                          progress.setValue(i);
                          QThread::msleep(100);
                          if (progress.wasCanceled())
                              break;
                          //... copy one file
                      }
                      progress.setValue(numFiles);
                  }
                  
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  ...
                  ...
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(UpdateRequest)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Paint)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(ShortcutOverride) // at here, i pressed the esc key.
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(WindowDeactivate)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(ActivationChange)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Hide)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(HideToParent)
                  QProgressDialog(0x28c5d4) ==== QEvent::Type(Leave)
                  QWidget(0x28c5d4) ==== QEvent::Type(PlatformSurface)
                  QWidget(0x28c5d4) ==== QEvent::Type(WinIdChange)
                  QWidget(0x28c5d4) ==== QEvent::Type(Destroy)
                  

                  I don't want let the esc key to close the QProgressdialog, How to do ?

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

                  @joeQ OK, I looked into it a bit more.

                  And from my understanding there is most likly a bug here.
                  That pressing ESC canceles the Dialog is a Shortcut defined in the constructor of the Dialog, -> not easy if at all to prevent /disable.

                  Therefor you would have to override the slot its connected to which is, void QProgressDialog::cancel(), which in my Test-program I was not able to override, at all....

                  So here's a work around from, me.

                  Disable the SIGNAL/SLOT connection of the cancel Signal in your constructor:

                  disconnect(this, SIGNAL(canceled()), this, SLOT(cancel()));
                  

                  and connect it to your custom myCancelSlot

                  connect(this, SIGNAL(canceled()), this, SLOT(myCancel)));
                  
                  void myCancel{
                    if(condition){
                       cancel();
                    }
                  }
                  

                  If you dont handle the canceled Signal yourself, than the only way to forcefully close the dialog would be by pressing the X-Button in the upper corner.

                  you should be able to work with that?


                  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.

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

                    @joeQ OK, I looked into it a bit more.

                    And from my understanding there is most likly a bug here.
                    That pressing ESC canceles the Dialog is a Shortcut defined in the constructor of the Dialog, -> not easy if at all to prevent /disable.

                    Therefor you would have to override the slot its connected to which is, void QProgressDialog::cancel(), which in my Test-program I was not able to override, at all....

                    So here's a work around from, me.

                    Disable the SIGNAL/SLOT connection of the cancel Signal in your constructor:

                    disconnect(this, SIGNAL(canceled()), this, SLOT(cancel()));
                    

                    and connect it to your custom myCancelSlot

                    connect(this, SIGNAL(canceled()), this, SLOT(myCancel)));
                    
                    void myCancel{
                      if(condition){
                         cancel();
                      }
                    }
                    

                    If you dont handle the canceled Signal yourself, than the only way to forcefully close the dialog would be by pressing the X-Button in the upper corner.

                    you should be able to work with that?

                    joeQJ Offline
                    joeQJ Offline
                    joeQ
                    wrote on last edited by
                    #10

                    @J.Hilk (^o^)/~,Thank u. Good idea. That is what i want. Thank u very much.

                    Just do it!

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      the_gollum
                      wrote on last edited by
                      #11

                      for me only overriding event(QEvent*) worked good:

                      protected:
                        bool event(QEvent* event)
                        {
                          QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                          if(keyEvent && keyEvent->key() == Qt::Key_Escape)
                          {
                            keyEvent->accept();
                            return true;
                          }
                          return QProgressDialog::event(event);
                        }
                      1 Reply Last reply
                      2

                      • Login

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