vitiral
vitiral

Reputation: 9248

Change text selection in PyQt QTextEdit

I'm trying to select different text in a QTextEdit object.

def get_text_cursor(self):
    return self.TextEdit.textCursor()

def get_text_selection(self):
    cursor = self.get_text_cursor()
    return cursor.selectionStart(), cursor.selectionEnd()

def set_text_selection(self, start, end):
    cursor = self.get_text_cursor()
    cursor.setPosition(start, end)
    self.TextEdit.setTextCursor(cursor)

This code does NOT work (get_text_selection does work) I have tried other things as well and they don't work either.

This question was already asked (but not really answered) here Select text of textEdit object with QTextCursor, QTextEdit

Working code, thanks to ekhumoro

# text cursor functions
def get_text_cursor(self):
    return self.TextEdit.textCursor()

def set_text_cursor_pos(self, value):
    tc = self.get_text_cursor()
    tc.setPosition(value, QtGui.QTextCursor.KeepAnchor)
    self.TextEdit.setTextCursor(tc)

def get_text_cursor_pos(self):
    return self.get_text_cursor().position()

def get_text_selection(self):
    cursor = self.get_text_cursor()
    return cursor.selectionStart(), cursor.selectionEnd()

def set_text_selection(self, start, end):
    cursor = self.get_text_cursor()
    cursor.setPosition(start)
    cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
    self.TextEdit.setTextCursor(cursor)

You can see this code in action at https://github.com/cloudformdesign/SearchTheSky

Upvotes: 1

Views: 11348

Answers (2)

bpreston
bpreston

Reputation: 11

I have subclassed the QPlainTextEdit and included these functions to add text select functionality. I have also added placeholder text deletion on first click and cursor placing wherever you want in the text. All you have to do is set flags for first input for the placeholder deletion. This will also work for QTextEdit.

class MyTextEdit(QtGui.QPlainTextEdit):

    def __init__(self, parent = None):

        super(MyTextEdit, self).__init__(parent)

        self.is_first_input = True

    def mousePressEvent(self, event):

        if self.is_first_input:
            self.selectAll()
            self.clear()
            self.is_first_input = False
        else:
            pass

        if event.button() == QtCore.Qt.LeftButton:

            self.startCursorPosition = event.pos()
            cursor = self.cursorForPosition(self.startCursorPosition)
            self.startPosition = cursor.position()


    def mouseMoveEvent(self, event):

        if event.button() == QtCore.Qt.NoButton:
            self.endCursorPosition = event.pos()
            cursor = self.cursorForPosition(self.endCursorPosition)
            position = cursor.position()
            cursor.setPosition(self.startPosition)
            cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor)
            self.setTextCursor(cursor)

    def mouseReleaseEvent(self, event):

        if event.button() == QtCore.Qt.LeftButton:

            self.endCursorPosition = event.pos()
            cursor = self.cursorForPosition(self.endCursorPosition)
            position = cursor.position()
            cursor.setPosition(self.startPosition)
            cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor)
            self.setTextCursor(cursor)

Upvotes: 1

ekhumoro
ekhumoro

Reputation: 120798

For QTextEdit, the selection is demarcated by the current postion and the anchor. But confusingly, although QTextCursor has a setPostion method for setting the current position, there is no corresponding setAnchor method for setting the anchor. So you have to call setPostion twice with a special flag:

    cursor = self.edit.textCursor()
    cursor.setPosition(start)
    cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
    self.TextEdit.setTextCursor(cursor)

Upvotes: 7

Related Questions