Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding detailed text in QMessageBox makes close (X) button disabled

Tags:

qt

qmessagebox

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

like image 285
frangulyan Avatar asked Sep 25 '11 02:09

frangulyan


3 Answers

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.

like image 106
Brown Avatar answered Sep 20 '22 14:09

Brown


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.

like image 32
mcdowella Avatar answered Sep 20 '22 14:09

mcdowella


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

like image 27
Gabriel C. Stabel Avatar answered Sep 21 '22 14:09

Gabriel C. Stabel