【问题标题】:QMessageBox prevent line breaks in DetailedTextQMessageBox 防止DetailedText中的换行符
【发布时间】:2020-08-12 17:06:43
【问题描述】:

我正在尝试构建一个消息对话框,显示对我的 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_()

【问题讨论】:

    标签: python qmessagebox qt.py


    【解决方案1】:

    您必须获取 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_()
    

    【讨论】:

    • 哇,太好了,有什么办法可以调整大小以适应?
    • @Jim 我明白你的问题是删除换行引起的不必要的换行符,对吗?如果是这样,那么如果您还有其他问题,请创建另一个帖子。
    • 这个问题的目的是防止换行,以便文本以全宽显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多