how to disable ESC key "close" the QProgressDialog ?
-
I use the
QProgressDialogin my project, but i find, when i pressed the esc key, theQProgressDialogclose. i just want to click thecancelButtonand then the dialog close.so, i subclass
QProgressDialog, and reimplement thecloseEvent, andkeyPressEvent. i can ignore the closeEvent. But, the esc key also can close the dialog.i read some reference. they said use the
eventFilterin 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. -
I use the
QProgressDialogin my project, but i find, when i pressed the esc key, theQProgressDialogclose. i just want to click thecancelButtonand then the dialog close.so, i subclass
QProgressDialog, and reimplement thecloseEvent, andkeyPressEvent. i can ignore the closeEvent. But, the esc key also can close the dialog.i read some reference. they said use the
eventFilterin 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.
-
@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); } -
@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 also tried it, also not work.
ProgressDialogis subclass ofQProgressDialogvoid 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.Hilk i also tried it, also not work.
ProgressDialogis subclass ofQProgressDialogvoid 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); } } -
@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,
ProgressDialogis subclass ofQProgressDialog. 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?
-
@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); } } -
@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 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.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
myCancelSlotconnect(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?
-
@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
myCancelSlotconnect(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?
-
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); }