无需在撤消堆栈中添加撤消命令即可对QTextEdit进行更改

use*_*225 4 qt pyside qtextedit

我正在寻找一种方式来改变QTextCharFormat一个的QTextEditQTextBlock不触发除了撤销命令。让我解释:

所述QTextCharFormatQTextBlock可以通过使用容易地改变QTextCursor::setBlockCharFormat()方法。假设我们有一个QTextEdit被调用的对象,myTextEdit它的可见光标在我们要更改的文本块内,我们可以QTextCharFormat像这样更改文本块:

text_cursor = myTextEdit.textCursor()
text_cursor.setBlockCharFormat(someNewCharFormat)
Run Code Online (Sandbox Code Playgroud)

上面的代码可以正常工作,但是还会向myTextEdit撤消堆栈中添加撤消命令。对于我自己而言,我希望能够改变QTextCharFormatQTextBlock 不添加撤销命令到QTextEdit的撤消堆栈。

我考虑使用该QTextDocument::setUndoRedoEnabled()方法暂时禁用撤消/重做系统,但是该方法也清除了撤消堆栈,我不想这样做。我还寻找了其他方法来更改撤消/重做系统的行为,但是我还没有找到一种方法来使其暂时忽略更改。我只是想在QTextEdit没有撤消/重做系统注册更改的情况下对a 进行更改。

任何提示或建议,不胜感激。谢谢!

Mar*_*k R 6

您必须将此与以前的修改分组。这很简单,你必须环绕代码是否与此修改:beginEditBlockendEditBlock。请参阅文档

text_cursor = myTextEdit.textCursor()
text_cursor.beginEditBlock()
text_cursor.setCharFormat(someOtherCharFormat) # some previous modification
text_cursor.setBlockCharFormat(someNewCharFormat)
text_cursor.endEditBlock()
Run Code Online (Sandbox Code Playgroud)

这样,对于任何复杂的修改,您都将对撤消堆栈进行一次提交。