QMessageBox prevent line breaks in DetailedText

Viewed 282

I am trying to build a message dialog that shows the details of impacts to my UI. This list is long enough that a scroll bar is needed but the text is long enough that I would prefer the lines to not be broken. Seems changign the size of QMessage dialog box is hard since it caluates it on its contents. Is there a way to "encourage that detailed box to prevent line breaks?

Alternatively allow resizing of the 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

1 Answers

You have to get the QTextEdit and disable the linewrap:

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

Related