pythonpyqt5qplaintexteditqtextcursor

Python PyQt5 - Selecting inserted cursor text?


I have a QPlainTextEdit widget, and am trying get the cursor to automatically select the inserted text. My current methodology is to select backwards using QTextCursor.WordLeft because using .insertText() moves the cursor to the end of that word. Thanks!

Edit: Further clarification: I ideally want the inserted text to become highlighted, with the cursor placed at the beginning of that inserted word. As an example: State 1 -> State 2

State 1 shows an input word. Then once the user hits the space-bar, the program inserts a word, highlights it, and places the cursor at the beginning of that inserted word, shown in State 2.

class TextBox(QPlainTextEdit):
    def __init__(self):
        QPlainTextEdit.__init__(self)

        font = QtGui.QFont()
        font.setPointSize(12)
        self.setFont(font)

    def keyPressEvent(self, keyEvent):
        super(TextBox, self).keyPressEvent(keyEvent)

        if keyEvent.key()  == Qt.Key_Return :
            self.clear()

        elif keyEvent.key() == Qt.Key_Space:
            cursor = self.get_cursor()
            cursor.insertText("test")    # The area of concern
            cursor.selectionStart()
            cursor.movePosition(QtGui.QTextCursor.WordLeft, QtGui.QTextCursor.KeepAnchor, 1)
            cursor.selectionEnd()
            # Moving the cursor position doesn't seem to do anything


    def get_cursor(self):
        return self.textCursor()

    def get_cursor_pos(self):
        return self.get_cursor().position()

Solution

  • What you're missing is that to apply the cursor position and selection, the cursor has to be set back to the text edit.

    class TextBox(QPlainTextEdit):
        # ...
    
        def keyPressEvent(self, keyEvent):
            super(TextBox, self).keyPressEvent(keyEvent)
    
            if keyEvent.key()  == Qt.Key_Return :
                self.clear()
    
            elif keyEvent.key() == Qt.Key_Space:
                cursor = self.textCursor()
                cursor.insertText("test")
                cursor.movePosition(QtGui.QTextCursor.WordLeft, QtGui.QTextCursor.KeepAnchor, 1)
                self.setTextCursor(cursor)
    

    Keep in mind that since you're calling the base class implementation of keyPressEvent, you'll always end up with a space before the "new" text. If, for any reason you want to avoid that, you'll have to ignore that whenever you get the space key.

        def keyPressEvent(self, keyEvent):
            if keyEvent.key()  == Qt.Key_Return :
                self.clear()
    
            elif keyEvent.key() == Qt.Key_Space:
                cursor = self.textCursor()
                pos = cursor.position()
                cursor.insertText("test")
                cursor.setPosition(pos, QtGui.QTextCursor.KeepAnchor)
                self.setTextCursor(cursor)
                # by returning, the event won't be sent to the default implementation
                return
    
            super(TextBox, self).keyPressEvent(keyEvent)