在PyQt QTextEdit中更改文本选择

时间:2013-11-14 22:45:28

标签: python qt pyqt

我试图在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

查看此代码

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分: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)