首页 > 解决方案 > 如何将 setPlaceholderText 包装在 QPlainTextEdit 字段中?

问题描述

我在该字段中有一个有点长的占位符文本,QPlainTextEdit但它不会环绕。即使我将setWordWrapModeattr 设置为WordWrap. 有没有办法做到这一点?

这是我在 Maya (2018) 中获得的屏幕截图:

在此处输入图像描述

文本应该阅读Comment on what you have done here, i.e.: what changes were made or new clothing...但在“是”之后被切断。你有点看到字母的尖端,但就是这样。

以下代码在 Maya 中不起作用:

import sys
from PySide2 import QtCore
from PySide2 import QtWidgets


class TestDialog(QtWidgets.QWidget):

    def __init__(self):
        super(TestDialog, self).__init__()

        self.setWindowTitle("testing")

        self.create_widgets()
        self.create_layout()

    def create_widgets(self):
        self.notes_ql = QtWidgets.QLabel()
        self.notes_ql.setText('Notes: ')
        self.notes_ql.setAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
        self.notes_pt = QtWidgets.QPlainTextEdit()
        self.notes_pt.setFixedHeight(100)
        self.notes_pt.setLineWrapMode(QtWidgets.QPlainTextEdit.WidgetWidth)
        self.notes_pt.verticalScrollBar().setValue(self.notes_pt.verticalScrollBar().minimum())
        self.notes_pt.setPlaceholderText('Comment on what you have done here, i.e.: what changes were made or new clothing...')


    def create_layout(self):
        notes_layout = QtWidgets.QHBoxLayout()
        notes_layout.addWidget(self.notes_ql)
        notes_layout.addWidget(self.notes_pt)

        main_layout = QtWidgets.QHBoxLayout(self)
        main_layout.setSpacing(1)
        main_layout.setContentsMargins(6, 6, 6, 6)
        main_layout.addLayout(notes_layout)


if __name__ == '__main__':
    try:
        test_dialog.close()  # pylint: disable=E0601
        test_dialog.deleteLater()
    except:
        pass

    test_dialog = TestDialog()
    test_dialog.show()
 

用下面的代码片段替换if __name__语句,使其作为独立应用程序运行:

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = TestDialog()
    window.show()
    app.exec_()

在此处输入图像描述

标签: pythonmayapyside2

解决方案


该代码适用于 Maya 2020,但不适用于 2019 或 2018。我的猜测是 Qt 5.6 (2018/2019) 或 Maya 的 PySide2 包中存在一个错误,该错误已在 Qt 5.12 (2020) 中修复。

不幸的是,这意味着早期版本可能没有简单的修复方法。


推荐阅读