0

我正在尝试在 QTextEdit 对象中选择不同的文本。

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)

此代码不起作用(get_text_selection确实起作用)我也尝试了其他方法,但它们也不起作用。

这个问题已经被问过(但没有真正回答)在这里 Select text of textEdit object with QTextCursor, QTextEdit

工作代码,感谢 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)

您可以在https://github.com/cloudformdesign/SearchTheSky查看此代码的运行情况

4

2 回答 2

5

对于 QTextEdit,选择由当前位置和锚点划分。但令人困惑的是,虽然 QTextCursor 有setPostion设置当前位置的方法,但是却没有对应setAnchor的设置锚点的方法。因此,您必须setPostion使用特殊标志调用两次:

    cursor = self.edit.textCursor()
    cursor.setPosition(start)
    cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
    self.TextEdit.setTextCursor(cursor)
于 2013-11-15T00:03:45.970 回答
1

我将 QPlainTextEdit 子类化并包含这些函数以添加文本选择功能。我还在第一次单击时添加了占位符文本删除,并将光标放置在文本中的任何位置。您所要做的就是为占位符删除的第一个输入设置标志。这也适用于 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)
于 2016-03-27T00:48:12.603 回答