QTextEdit
方法轻松更改A的
QTextBlock
。 假设我们有一个叫
QTextCharFormat
的
QTextBlock
QTextCursor::setBlockCharFormat()
,其可见光标在我们要更改的文本块中,我们可以更改文本块的
QTextEdit
myTextEdit
这样:
QTextCharFormat
上面的代码正常运行,但它也会在
text_cursor = myTextEdit.textCursor()
text_cursor.setBlockCharFormat(someNewCharFormat)
撤消堆栈中添加一个撤消命令。 出于我自己的目的,我希望能够更改一个
myTextEdit
QTextCharFormat
的QTextBlock
QTextEdit
,而无需在
QTextDocument::setUndoRedoEnabled()
的撤消堆栈中添加撤消命令。I考虑使用
QTextEdit
方法暂时禁用撤消/重做系统,但是该方法也清除了撤消堆栈,我不想这样做。 我还寻找了其他方法来改变撤消/重做系统的行为,但是我还没有找到一种使它暂时忽略变化的方法。 我只想在没有撤消/重做系统的情况下进行更改。任何提示或建议。 谢谢!
您必须将其与以前的修改分组。您必须简单地包围代码,该代码可以通过以下方式进行此修改:
beginEditBlock
和
endEditBlock
。请参阅documentation
.text_cursor = myTextEdit.textCursor()
text_cursor.beginEditBlock()
text_cursor.setCharFormat(someOtherCharFormat) # some previous modification
text_cursor.setBlockCharFormat(someNewCharFormat)
text_cursor.endEditBlock()
这种方式将对任何复杂修改进行单一提交撤消堆栈。joinpreviousblock()应该做到的技巧:
cursor = self.textCursor()
cursor.joinPreviousEditBlock()
cursor.setPosition(start, QTextCursor.MoveAnchor)
cursor.setPosition(end, QTextCursor.KeepAnchor)
cursor.setCharFormat(fmt)
cursor.endEditBlock()
您应该使用QSyntaxHighlighter
。扩展它并实现setFormat
文献。
。它是低级别的API,其func不会产生任何撤消堆栈。
setAdditionalFormats
这也用于qsyntaxhighlighter的内部。