3

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jim
  • 2,034
  • 2
  • 15
  • 29

1 Answers1

3

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Wow that's pretty good, any way to make it resize to fit? – Jim Aug 12 '20 at 17:28
  • @Jim I understood that your question is to remove the unnecessary line breaks caused by the line wrap, am I correct? If so then if you have another question then create another post. – eyllanesc Aug 12 '20 at 17:30
  • The intent of the question was to prevent line breaks so that the text will be shown at full width – Jim Aug 12 '20 at 17:36