PDA

View Full Version : disable close button in QMessageBox



mentalmushroom
4th May 2011, 15:01
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.


Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::CustomizeWindowHint

also tried this with the same effect:

setWindowFlags(Qt::FramelessWindowHint);
setWindowFlags(Qt::WindowTitleHint);

MasterBLB
4th May 2011, 16:16
Disable Qt::WindowCloseButtonHint flag

mentalmushroom
4th May 2011, 16:20
QMessageBox mbox(QMessageBox::Question, "my app", tr("Do you want to update?"), QMessageBox::Yes | QMessageBox::No, &w,
Qt::WindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::CustomizeWindowHint) & ~Qt::WindowCloseButtonHint);


Doesn't work. Or am I doing something wrong?

falconium
4th May 2011, 16:33
Try to remove Qt::WindowSystemMenuHint from the list. On some system it implies close button, too.

mentalmushroom
4th May 2011, 16:35
no, didn't help either

falconium
4th May 2011, 17:00
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.


QDialog mbox;
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.

nmahoney
3rd June 2011, 15:26
I found this thread because I had the same problem, but managed to solve it in a different way.


QMessageBox mBox;
mBox.setWindowTitle("Dialog Title");
mBox.setText("Question?");
mBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);

Qt::WindowFlags wFlags = mBox.windowFlags();
if(Qt::WindowCloseButtonHint == (wFlags & Qt::WindowCloseButtonHint))
{
wFlags = wFlags ^ Qt::WindowCloseButtonHint;
mBox.setWindowFlags(wFlags);
}

wysota
3rd June 2011, 17:41
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.

SixDegrees
4th June 2011, 11:59
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.

wysota
4th June 2011, 21:08
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.