"Replace all" 后撤消
Undo after "Replace all"
我实现了一个 "replace all" 函数,当按下 "Find and replace" window 上的全部替换按钮时会触发该函数。
但是,如果我尝试使用内置撤消功能撤消更改,则什么也不会发生。
出现对话框 window 时,是否与我的文本编辑器没有聚焦有关?
def handle_replace_all():
old = find_win.line_edit_find.text() # text to replace
new = find_win.line_edit_replace.text() # new text
cursor = self.text_edit.textCursor()
cursor.beginEditBlock()
current_text = self.text_edit.toPlainText()
replaced_text = current_text.replace(old, new)
self.text_edit.setPlainText(replaced_text)
cursor.endEditBlock()
find_window.button_replace_all.clicked.connect(handle_replace_all)
为什么会这样?
感谢任何帮助。
如果您想使用撤消-重做功能,您必须仅使用 QTextCursor
:
进行修改
def handle_replace_all(self):
old = find_win.line_edit_find.text() # text to replace
new = find_win.line_edit_replace.text() # new text
current_text = self.text_edit.toPlainText()
replaced_text = current_text.replace(old, new)
cursor = self.text_edit.textCursor()
cursor.beginEditBlock()
cursor.select(QtGui.QTextCursor.Document)
cursor.removeSelectedText()
cursor.insertText(replaced_text)
cursor.endEditBlock()
我实现了一个 "replace all" 函数,当按下 "Find and replace" window 上的全部替换按钮时会触发该函数。 但是,如果我尝试使用内置撤消功能撤消更改,则什么也不会发生。
出现对话框 window 时,是否与我的文本编辑器没有聚焦有关?
def handle_replace_all():
old = find_win.line_edit_find.text() # text to replace
new = find_win.line_edit_replace.text() # new text
cursor = self.text_edit.textCursor()
cursor.beginEditBlock()
current_text = self.text_edit.toPlainText()
replaced_text = current_text.replace(old, new)
self.text_edit.setPlainText(replaced_text)
cursor.endEditBlock()
find_window.button_replace_all.clicked.connect(handle_replace_all)
为什么会这样? 感谢任何帮助。
如果您想使用撤消-重做功能,您必须仅使用 QTextCursor
:
def handle_replace_all(self):
old = find_win.line_edit_find.text() # text to replace
new = find_win.line_edit_replace.text() # new text
current_text = self.text_edit.toPlainText()
replaced_text = current_text.replace(old, new)
cursor = self.text_edit.textCursor()
cursor.beginEditBlock()
cursor.select(QtGui.QTextCursor.Document)
cursor.removeSelectedText()
cursor.insertText(replaced_text)
cursor.endEditBlock()