将QTEXTEDIT的更改不添加撤消命令 我正在寻找一种方法来更改QTEXTEDIT QTEXTBLOCK的QTEXTCHURFORFAT,而不会触发添加撤消命令。让我解释一下: QTextBlock的QTEXTCHURFORMAT可以很容易地

问题描述 投票:0回答:3
可以使用

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

.
qt pyside qtextedit
3个回答
8
投票
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

。扩展它并实现

3
投票
函数,并在其中调用

setFormat

以更改格式,而无需进行撤消/重做堆栈。有关更多详细信息,请参见
文献

1
投票
QTextlayout

。它是低级别的API,其func不会产生任何撤消堆栈。 setAdditionalFormats

这也用于qsyntaxhighlighter的内部。
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.