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