简体   繁体   English

QT QSyntaxHighlighter - 什么是setCurrentBlockState角色?

[英]QT QSyntaxHighlighter - what's the setCurrentBlockState role?

Maybe I'm simply dumb, but I've read the documentation of the qt richtext demo here ( https://doc.qt.io/archives/qt-4.7/richtext-syntaxhighlighter.html ) and the setCurrentBlockState documentation here ( https://doc.qt.io/archives/qt-4.7/qsyntaxhighlighter.html#setCurrentBlockState ) but I still can't figure out what's the meaning how the "newState" and how these functions are used in the "highlightBlock" event. 也许我只是愚蠢,但我在这里阅读了qt richtext演示文档( https://doc.qt.io/archives/qt-4.7/richtext-syntaxhighlighter.html )和这里的setCurrentBlockState文档( https ://doc.qt.io/archives/qt-4.7/qsyntaxhighlighter.html#setCurrentBlockState )但我仍然无法弄清楚“newState”是什么意思以及如何在“highlightBlock”事件中使用这些函数。

Can someone enlighten me with simple words please? 有人可以用简单的话来启发我吗?

As far as Qt is concerned, newState has no meaning; 就Qt而言, newState没有意义; it is an arbitrary integer. 它是一个任意整数。 When you call setCurrentBlockState , QSyntaxHighlighter will merely associate the newState integer with the current block of text that you are working with inside the highlightBlock member function. 当您调用setCurrentBlockStateQSyntaxHighlighter将仅将newState整数与您在highlightBlock成员函数中使用的当前文本块相关联。 In other words, the integer is strictly a user value that you can use to mean anything you want. 换句话说,整数严格来说是一个用户值,可以用来表示你想要的任何东西。

As mentioned in the example you posted, the problem with highlightBlock is that it only really knows about the current block of text that the rich text engine has passed into it. 正如您发布的示例中所提到的, highlightBlock的问题在于它只知道富文本引擎传递给它的当前文本块。 If you need to know something about the previous block of text that you processed, you need to store this information somewhere. 如果您需要了解有关您处理的上一个文本块的信息,则需要将此信息存储在某处。 As such, the setCurrentBlockState function was added such that you could store some arbitrary information about the current block of text that you are working with such that a later invocation of highlightBlock could see what the previous invocation was doing. 因此,添加了setCurrentBlockState函数,以便您可以存储有关您正在使用的当前文本块的一些任意信息,以便稍后调用highlightBlock可以查看先前调用正在执行的操作。 In the example, it was mentioned that highlighting a multi-line C++ comment would require functionality like this to work properly. 在该示例中,有人提到突出显示多行C ++注释需要这样的功能才能正常工作。

All of that being said, you don't actually have to use setCurrentBlockState . 所有这些,你实际上不必使用setCurrentBlockState You can implement your own means of storing state between highlightBlock invocations. 您可以在highlightBlock调用之间实现自己的状态存储方式。 However, setCurrentBlockState provides an efficient means of associating an integer with a block of text. 但是, setCurrentBlockState提供了一种将整数与文本块相关联的有效方法。

Hopefully this makes things clear. 希望这能使事情变得清晰。

I think I have come to realize that using setCurrentBlockState() to remember the multiline highlighting state will not be sufficient in case you have multiple rules for multiline highlightning. 我想我已经意识到使用setCurrentBlockState()来记住多行突出显示状态将不足以防止你有多个多行高亮显示规则。 Let's say you have some kind of file where you have different kinds of multiline statement and need to color those differently. 假设您有某种文件,其中您有不同类型的多行语句,需要对它们进行不同的着色。 Then you need to maintain a current and previous state for each of them. 然后,您需要为每个状态维护当前和之前的状态。 It wasn't enough that I let the multiline's rule index (+1) represent the current highlighting state and then match that index when called for the next block. 仅仅让多线的规则索引(+1)表示当前的突出显示状态,然后在调用下一个块时匹配该索引是不够的。 In that case, one has to maintain the state information for each and every multiline rule. 在这种情况下,必须维护每条多线规则的状态信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM