在QMessageBox中添加详细文本使关闭(X)按钮处于禁用状态 [英] Adding detailed text in QMessageBox makes close (X) button disabled

查看:1043
本文介绍了在QMessageBox中添加详细文本使关闭(X)按钮处于禁用状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到了一件有趣的事情-如果我向QMessageBox添加了详细的文本(其中添加了"Show Details ..."按钮),则执行该命令将显示系统框架的关闭(X)按钮被禁用,因此将该窗口标记为非-closesable(右键单击框架->禁用关闭").

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).

以下是一些示例代码:

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

我什至没有找到在Qt中手动执行此操作的方法.有什么想法吗?

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

谢谢

推荐答案

我在Python 2.7和PySide上遇到了同样的问题.

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.

这篇关于在QMessageBox中添加详细文本使关闭(X)按钮处于禁用状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆