我正在尝试在 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查看此代码的运行情况