how to disable ESC key "close" the QProgressDialog ?



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



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



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


  • @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
    }
    
    


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


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



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



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.