微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

QMessageBox防止在DetailsText

如何解决QMessageBox防止在DetailsText

我正在尝试建立一个消息对话框,以显示对UI的影响的详细信息。该列表足够长,需要滚动条,但是文本足够长,我希望这些行不被打断。似乎很难更改QMessage对话框的大小,因为它会根据其内容进行计算。有没有一种方法可以“鼓励详细框防止换行?

或者允许调整QMessageBox的大小

impacts = []
# Create Impacts
for i in range(0,100):
    impacts.append(" This is a text can be a little long but not too long impact {}".format(i))

# CreateDialog
diffBox = QMessageBox()
diffBox.setwindowTitle("Test")
diffBox.setinformativeText(
    "Impacts have been found,and this message is long but not too long as well but independent of the list")
diffBox.setDetailedText("changes:\n\n" + "\n".join(impacts))

# Add Buttons
diffBox.addButton("Apply",QMessageBox.AcceptRole)
diffBox.setStandardButtons(QMessageBox.Cancel)
diffBox.setSizegripEnabled(True)
result = diffBox.exec_()

enter image description here

enter image description here

解决方法

您必须获取QTextEdit并禁用换行:

from Qt.QtCore import Qt
from Qt.QtWidgets import QApplication,QMessageBox,QTextEdit


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    impacts = []
    # Create Impacts
    for i in range(0,100):
        impacts.append(
            " This is a text can be a little long but not too long impact {}".format(i)
        )

    # CreateDialog
    diffBox = QMessageBox()
    diffBox.setWindowTitle("Test")
    diffBox.setInformativeText(
        "Impacts have been found,and this message is long but not too long as well but independent of the list"
    )
    diffBox.setDetailedText("changes:\n\n" + "\n".join(impacts))

    # Add Buttons
    diffBox.addButton("Apply",QMessageBox.AcceptRole)
    diffBox.setStandardButtons(QMessageBox.Cancel)
    diffBox.setSizeGripEnabled(True)

    te = diffBox.findChild(QTextEdit)
    if te is not None:
        te.setLineWrapMode(QTextEdit.NoWrap)
        te.parent().setFixedWidth(
            te.document().idealWidth()
            + te.document().documentMargin()
            + te.verticalScrollBar().width()
        )

    result = diffBox.exec_()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。