Question

I noticed an interesting thing - if I add a detailed text to QMessageBox (which adds "Show Details..." button) then executing it will show the system frame's close (X) button disabled and hence marking this window as non-closable (right click on frame -> Close disabled).

Here is some sample code:

QMessageBox box(QMessageBox::Critical, title, text, QMessageBox::Ok);
box.setDetailedText(detailedText); // comment this line to get close button enabled
box.exec();

I didn't even find a way to manually do this in Qt. Any ideas?

Thanks

Was it helpful?

Solution

I was having the same problem with Python 2.7 and PySide.

In this example, the red close button works as expected:

from PySide import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)

message_box = QtGui.QMessageBox()
message_box.setWindowTitle("Close Test")
message_box.setText("Testing whether or not the red X is enabled.")

ret = message_box.exec_()

Adding detailed text disables the close button:

from PySide import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)

message_box = QtGui.QMessageBox()
message_box.setWindowTitle("Close Test")
message_box.setText("Testing whether or not the red X is enabled.")
message_box.setDetailedText("These details disable the close button for some reason.")

ret = message_box.exec_()

The answer marked as the solution does not solve this problem. As you can see in this example, the close button remains disabled:

from PySide import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)

message_box = QtGui.QMessageBox()
message_box.setWindowTitle("Close Test")
message_box.setText("Testing whether or not the red X is enabled.")
message_box.setDetailedText("These details disable the close button for some reason.")

message_box.setWindowFlags(message_box.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint)

ret = message_box.exec_()

The answer is to set the standard buttons and ALSO set the escape button:

from PySide import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)

message_box = QtGui.QMessageBox()
message_box.setWindowTitle("Close Test")
message_box.setText("Testing whether or not the red X is enabled.")
message_box.setDetailedText("These details disable the close button for some reason.")

message_box.setStandardButtons(QtGui.QMessageBox.Ok)
message_box.setDefaultButton(QtGui.QMessageBox.Ok)
message_box.setEscapeButton(QtGui.QMessageBox.Ok)

ret = message_box.exec_()

This restores the desired close button behavior.

OTHER TIPS

I came across this recently on Qt 4.8 Linux. I found that whether or not the X was disabled depended on the ButtonRole I used on the call to QMessageBox::addButton(). The X was disabled when all roles were ActionRole - which is really supposed to be for buttons that affect the dialog, but do not accept or reject it. What the buttons did in my case is more accurately described as AcceptRole or RejectRole. When I changed the roles to have one RejectRole and the rest AcceptRole, the X started working. It looks as though QMessageBox was reluctant to accept a close when none of the buttons had roles that mapped to close.

You need to unset the Qt::WindowCloseButtonHint widget flag. Like this:

QMessageBox messageBox;
messageBox.setWindowFlags(messageBox.windowFlags() & ~Qt::WindowCloseButtonHint);

You may unset this flag Qt::WindowSystemMenuHint either.

Adds a window system menu, and possibly a close button (for example on Mac). If you need to hide or show a close button, it is more portable to use WindowCloseButtonHint.

http://qt-project.org/doc/qt-4.8/qt.html#WindowType-enum

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top