簡體   English   中英

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

[英]How to insert text at the top of a QTextEdit widget?

我有一個簡單的QtextEdit表單,我將其用作一種日志。 事件被記錄到表單中,因此用戶可以查看歷史事件。 我正在使用textEdit.append()向表單添加新行。 但是textEdit.append()會將文本追加到緩沖區的底部,因此最新事件顯示在底部,是否有任何合理的方法追加到頂部,所以最新事件顯示在頂部?

謝謝。

您可以使用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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM