How to disable all options of QDialog closure
-
wrote on 29 Jan 2021, 20:42 last edited by
Hey
I'm in need of a QDialog that would not be closed by anything user do. How can I properly set up the widget to do it?
I tried capturing closeEvent() but esc seem to skip it somehow?
Any hints?
TIA
-
Hi
Docs says
In order to modify your dialog's close behavior, you can reimplement the functions accept(), reject() or done(). The "closeEvent() function should only be reimplemented to preserve the dialog's position or to override the standard close or reject behavior."So i think you should override accept(), reject() and handle it that way.
-
wrote on 30 Jan 2021, 11:03 last edited by
Hello!
Additionally, you can try to make the dialog without controls by setting the code below:
setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
The user can not minimize/maximize or close such dialog:
Also, you can modify the
nativeEvent
to intercept the dialog closure: https://forum.qt.io/topic/104533/how-to-ignore-the-event-when-i-click-on-the-button-to-maximize-the-window-windowmaximizebuttonhint/9Happy coding!
-
Hello!
Additionally, you can try to make the dialog without controls by setting the code below:
setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
The user can not minimize/maximize or close such dialog:
Also, you can modify the
nativeEvent
to intercept the dialog closure: https://forum.qt.io/topic/104533/how-to-ignore-the-event-when-i-click-on-the-button-to-maximize-the-window-windowmaximizebuttonhint/9Happy coding!
wrote on 30 Jan 2021, 11:07 last edited by JonB@Cobra91151 said in How to disable all options of QDialog closure:
The user can not minimize/maximize or close such dialog:
I cannot test this, but OOI what does pressing Alt+F4 under Windows on this dialog do?
-
@Cobra91151 said in How to disable all options of QDialog closure:
The user can not minimize/maximize or close such dialog:
I cannot test this, but OOI what does pressing Alt+F4 under Windows on this dialog do?
wrote on 30 Jan 2021, 11:21 last edited byHello! Here is how it intercepts the
Alt+F4
.So, as you can see, setting window flags and overriding the native event will help to prevent the dialog from closing.
-
Hello! Here is how it intercepts the
Alt+F4
.So, as you can see, setting window flags and overriding the native event will help to prevent the dialog from closing.
wrote on 30 Jan 2021, 11:37 last edited by JonB@Cobra91151 said in How to disable all options of QDialog closure:
and overriding the native event
Indeed! My question was whether this was required to make it work robustly; if you only do the
setWindowFlags()
I wondered whether the keyboard shortcut would still cause closure? -
@Cobra91151 said in How to disable all options of QDialog closure:
and overriding the native event
Indeed! My question was whether this was required to make it work robustly; if you only do the
setWindowFlags()
I wondered whether the keyboard shortcut would still cause closure?wrote on 30 Jan 2021, 11:50 last edited byEven when
setWindowFlags
are set, you have to intercept the dialog closure using thenativeEvent
.Code:
bool TestDlg::nativeEvent(const QByteArray &eventType, void *message, long *) { qDebug() << eventType; MSG *msg = static_cast<MSG *>(message); #ifdef __linux__ //Linux code here #elif _WIN32 if (msg->message == WM_SYSCOMMAND) { if (msg->wParam == SC_CLOSE) { QMessageBox::information(this, "Test", "Closing...", QMessageBox::Ok); //Add your code here return true; } } #endif return false; }
-
Even when
setWindowFlags
are set, you have to intercept the dialog closure using thenativeEvent
.Code:
bool TestDlg::nativeEvent(const QByteArray &eventType, void *message, long *) { qDebug() << eventType; MSG *msg = static_cast<MSG *>(message); #ifdef __linux__ //Linux code here #elif _WIN32 if (msg->message == WM_SYSCOMMAND) { if (msg->wParam == SC_CLOSE) { QMessageBox::information(this, "Test", "Closing...", QMessageBox::Ok); //Add your code here return true; } } #endif return false; }
wrote on 30 Jan 2021, 11:54 last edited by JonB@Cobra91151
Which is what I suspected, hence the question.Your original answer made the
nativeEvent
interception look "optional" ("you can modify"), now the OP knows it is mandatory.
1/8