首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >QMessageBox在DetailedText中防止换行

QMessageBox在DetailedText中防止换行
EN

Stack Overflow用户
提问于 2020-08-13 01:06:44
回答 1查看 147关注 0票数 2

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

或者,允许调整QMessageBox的大小

代码语言:javascript
运行
复制
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_()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-13 01:18:17

您必须获取QTextEdit并禁用线缆:

代码语言:javascript
运行
复制
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_()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63381495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档