Pyqt5 QTextEdit 光标移动 选择 删除

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_())

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值