如何在 QTextEdit 小部件的顶部插入文本?

问题描述 投票:0回答:1

我有一个简单的

QTextEdit
表单,我将其用作一种日志。事件被记录在表单中,以便用户可以查看历史事件。 我正在使用
textEdit.append()
向表单添加新行。 然而,
textEdit.append()
,将文本附加到缓冲区的末尾,因此最新的事件显示在底部。是否有合理的方法附加到开头,以便最新的事件显示在顶部?

python insert pyqt append qtextedit
1个回答
4
投票

您可以使用

insertPlainText
方法在当前文本中的任意位置插入文本。放置光标以指定要插入文本的位置。在你的情况下,你可以把它放在开头:

from PyQt5.QtGui import QTextCursor

# set the cursor position to 0
cursor = QTextCursor(textEdit.document())
# set the cursor position (defaults to 0 so this is redundant)
cursor.setPosition(0)
textEdit.setTextCursor(cursor)

# insert text at the cursor
textEdit.insertPlainText('your text here')
© www.soinside.com 2019 - 2024. All rights reserved.