PySide,如何设置QMessageBox文本格式?

PySide, how to setup QMessageBox text format?

我想设置QMessageBox文本格式,但是找不到有效的方法。

QtGui.QMessageBox.information(
    self, 
    "Confirm delete!", 
    "Are you sure you want to delete file?\n %s" % filename,
    QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
    QtGui.QMessageBox.No
)

我想加粗,比如:

"Are you sure you want to delete file?\n <b>%s</b>"

我该怎么做?

根据 PySide 的文档,您可以使用 setTextFormat 来指定 Qt.RichText 格式。

然而,这仅在您创建自定义 QMessageBox 实例 并进行设置时有效,在使用 QMessageBox.information 静态时无效-方法。

默认QMessageBox使用Qt.AutoText格式,通过Qt.mightBeRichText函数尝试检测文本是否为富文本(我只能找到Qt的文档)。该函数的文档说明:

Returns true if the string text is likely to be rich text; otherwise returns false.

This function uses a fast and therefore simple heuristic. It mainly checks whether there is something that looks like a tag before the first line break. Although the result may be correct for common cases, there is no guarantee.

不幸的是,在您的消息中,您有换行符 \n 任何标记之前,因此该函数无法将您的消息检测为富文本格式。

在任何情况下,一旦您将文本解释为富文本,就必须使用 HTML 换行符 <br> 因此使用消息:

"Are you sure you want to delete file?<br> <b>%s</b>"

将使 QMessageBox 自动检测富文本并生成您想要的结果。