disable close button in QMessageBox
Is it possible to disable close button in QMessageBox? I want to make a question box where only Yes and No will be enabled.
I tried to find out the needed flags with Qt "windowflags" example, the following combination seems to work for the window in that sample application, but it doesn't work with QMessageBox.
Code:
Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::CustomizeWindowHint
also tried this with the same effect:
Code:
setWindowFlags(Qt::FramelessWindowHint);
setWindowFlags(Qt::WindowTitleHint);
Re: disable close button in QMessageBox
Disable Qt::WindowCloseButtonHint flag
Re: disable close button in QMessageBox
Code:
Qt::WindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::CustomizeWindowHint) & ~Qt::WindowCloseButtonHint);
Doesn't work. Or am I doing something wrong?
Re: disable close button in QMessageBox
Try to remove Qt::WindowSystemMenuHint from the list. On some system it implies close button, too.
Re: disable close button in QMessageBox
Re: disable close button in QMessageBox
Bad news: it looks like you will need to implement a special message box from QDialog, because of QMessageBox doesn't let you to restrict close button by its nature.
Code:
mbox.setWindowFlags(Qt::WindowTitleHint | Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::CustomizeWindowHint);
mbox.exec();
It works, but you will also need to restrict key ESC, and put Yes/No buttons manually.
Re: disable close button in QMessageBox
I found this thread because I had the same problem, but managed to solve it in a different way.
Code:
mBox.setWindowTitle("Dialog Title");
mBox.setText("Question?");
Qt::WindowFlags wFlags = mBox.windowFlags();
if(Qt::WindowCloseButtonHint == (wFlags & Qt::WindowCloseButtonHint))
{
wFlags = wFlags ^ Qt::WindowCloseButtonHint;
mBox.setWindowFlags(wFlags);
}
Re: disable close button in QMessageBox
The standard way is to allow the user to close the dialog using regular means and interpret this as if he pushed one of the buttons (usually the one related to "rejecting" the dialog -- most likely the "No" button in this example). The same goes with handling the Esc key.
Re: disable close button in QMessageBox
Quote:
Originally Posted by
wysota
The standard way is to allow the user to close the dialog using regular means and interpret this as if he pushed one of the buttons (usually the one related to "rejecting" the dialog -- most likely the "No" button in this example). The same goes with handling the Esc key.
Agree. Subverting normal UI conventions is very bad form. The user expects things to work as they always have, and will be frustrated if they don't.
Re: disable close button in QMessageBox
To explain further what I mean...
Usually you want to disable the close button because you don't want the user to be able to close the window. Here the situation is different -- you want him to close the window (which will happen if he chooses a button from the dialog box) but only using the means chosen by you and not any other. There is no reason to do that.