Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python QMessageBox在DetailedText中防止换行_Python_Qmessagebox_Qt.py - Fatal编程技术网

Python QMessageBox在DetailedText中防止换行

Python QMessageBox在DetailedText中防止换行,python,qmessagebox,qt.py,Python,Qmessagebox,Qt.py,我正在尝试构建一个消息对话框,显示对我的UI的影响的详细信息。这个列表足够长,需要一个滚动条,但是文本足够长,我希望行不要被打断。QMessage对话框的大小似乎很难调整,因为它会根据其内容进行计算。有没有办法“鼓励那个详细的方框来防止断线?” 或者,允许调整QMessageBox的大小 impacts = [] # Create Impacts for i in range(0, 100): impacts.append(" This is a text can be a l

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


您必须获取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我知道你的问题是删除换行造成的不必要的换行符,对吗?如果是这样,如果你还有其他问题,那么就创建另一篇文章。这个问题的目的是防止换行符,以便文本以全宽显示