1. 移动光标 但不选择 MoveAnchor
text_cursor = self.text_edit.textCursor() # 光标获取
text_cursor.movePosition(QTextCursor.MoveOperation.End, QTextCursor.MoveMode.MoveAnchor) # 移动光标
self.text_edit.setTextCursor(text_cursor) # 更改光标
2. 移动光标 并且选择 KeepAnchor
text_cursor.movePosition(QTextCursor.MoveOperation.StartOfLine, QTextCursor.MoveMode.KeepAnchor)
如果光标移动到文档尾部,再移动到行首并 KeepAnchor,会选择最后一行,之后即可实现删除
text_cursor = self.text_edit.textCursor()
text_cursor.deleteChar()
self.text_edit.setTextCursor(text_cursor)
deleteChar() 会删除当前选择的内容。
完整测试代码:
from PyQt5.QtWidgets import QWidget, QTextEdit, QHBoxLayout, QPushButton, QApplication
from PyQt5.QtGui import QTextCursor
import sys
class TestQTextEdit(QWidget):
def __init__(self):
super().__init__()
# TextEdit
self.text_edit = QTextEdit(self)
self.text_edit.setGeometry(0, 100, 800, 800)
self.text_edit.setText("dddddjjjjjjjjjjjjddddddd\n555555777777777555555555\ndggggggggggkkkkkkkkkkggggggggg\nttttttt66666666tttt")
self.text_edit.setStyleSheet('font-size:24px;')
self.setting_widget = QWidget(self)
self.vertical_setting_layout = QHBoxLayout()
self.button_move_cursor_to_end = QPushButton("移动光标至文档尾")
self.button_move_cursor_to_end.clicked.connect(self.move_cursor_to_document_end)
self.button_move_cursor_to_line_start_and_keep_select = QPushButton("移动光标至行首并选择")
self.button_move_cursor_to_line_start_and_keep_select.clicked.connect(self.move_cursor_to_line_start_and_keep_select)
self.button_move_cursor_to_line_end = QPushButton("移动光标至行尾")
self.button_move_cursor_to_line_end.clicked.connect(self.move_cursor_to_line_end)
self.button_delete_current_select = QPushButton("删除选中")
self.button_delete_current_select.clicked.connect(self.delete_current_select)
self.vertical_setting_layout.addWidget(self.button_move_cursor_to_end)
self.vertical_setting_layout.addWidget(self.button_move_cursor_to_line_end)
self.vertical_setting_layout.addWidget(self.button_move_cursor_to_line_start_and_keep_select)
self.vertical_setting_layout.addWidget(self.button_delete_current_select)
self.setting_widget.setLayout(self.vertical_setting_layout)
self.setting_widget.setGeometry(0, 0, 800, 100)
self.setting_widget.setStyleSheet('font-size:22px;')
def move_cursor_to_document_end(self):
text_cursor = self.text_edit.textCursor()
text_cursor.movePosition(QTextCursor.MoveOperation.End, QTextCursor.MoveMode.MoveAnchor)
self.text_edit.setTextCursor(text_cursor)
def move_cursor_to_line_start_and_keep_select(self):
text_cursor = self.text_edit.textCursor()
text_cursor.movePosition(QTextCursor.MoveOperation.StartOfLine, QTextCursor.MoveMode.KeepAnchor)
self.text_edit.setTextCursor(text_cursor)
def move_cursor_to_line_end(self):
text_cursor = self.text_edit.textCursor()
text_cursor.movePosition(QTextCursor.MoveOperation.EndOfLine, QTextCursor.MoveMode.MoveAnchor)
self.text_edit.setTextCursor(text_cursor)
def delete_current_select(self):
text_cursor = self.text_edit.textCursor()
text_cursor.deleteChar()
self.text_edit.setTextCursor(text_cursor)
if __name__ == '__main__':
app = QApplication(sys.argv)
chat_window = TestQTextEdit()
chat_window.setGeometry(0, 0, 800, 800)
chat_window.show()
sys.exit(app.exec_())