【问题标题】:Undo after "Replace all"“全部替换”后撤消
【发布时间】:2019-12-19 10:48:39
【问题描述】:

我已经实现了一个“全部替换”功能,当按下“查找和替换”窗口上的全部替换按钮时会触发该功能。 但是,如果我尝试使用内置撤消功能撤消更改,则不会发生任何事情。

是否与我的文本编辑器在对话框窗口显示时没有聚焦有关?

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)

为什么会这样? 感谢任何帮助。

【问题讨论】:

标签: python pyqt pyqt5 undo qtextedit


【解决方案1】:

如果您想使用撤消重做功能,您必须仅使用 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()

【讨论】:

  • 这正是我想要的!非常感谢。
猜你喜欢
  • 2021-08-11
  • 2013-01-02
  • 2016-02-23
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 2014-06-17
  • 1970-01-01
  • 2013-06-11
相关资源
最近更新 更多