Solved QSyntaxHighlighter hardly threadable
-
Re: How to avoid QSyntaxHighlighter::highlightBlock blocking the UI thread
Hi all,
like Krantz I've got the problem that the QSyntaxHighlighter blocks the ui while processing. I want to highlight really huge source files.
The easiest way to break the chain would be to tell the highlighter just to highlight a bunch of lines, passing the processing back to do some ui things and then getting triggered to process the next bunch of lines. In case of changes in between this might be reprocessing some prior lines.
But... I don't see any hook to get in there and stop the highlighter from doing all lines at once.
The alternative plan which I'd like to avoid would be to copy my bunch of lines to an internal QTextDocument and have the highlighter only working on that. After processing the formats can be assigned to the big document. I don't feel really good with that overhead of copying.
Any idea of doing that better?
Regards
-
Hi,
You should likely take a look at the sources of QSyntaxHighlighter, you may find there how to accomplish what you want.
-
Hi SGaist,
I already read the sources. The only hook I found would be to suppress executing rehighlight by unsetting rehighlightPending. But I failed to get access to that flag of the private internal class.
I think it's best to write my own interruptable highlighter as it seems to be a rather small class. Maybe I suggest adding a possibility to pause and resume the highlighting process to the qt team.
Regards
-
Hi @jazzco2,
Maybe I suggest adding a possibility to pause and resume the highlighting process to the qt team.
You can file a suggestion at bugreports.qt.io (and please add a link to that here).
Sometimes the developers can give you a hint for a solution too. -
highlightBlock(const QString &text) is really fast because cut huge file in many blocks.
Maybe your process inside is not optimised ? do you use QRegularExpression ? -
@cfdev said in QSyntaxHighlighter hardly threadable:
highlightBlock(const QString &text) is really fast because cut huge file in many blocks.
Maybe your process inside is not optimised ? do you use QRegularExpression ?No, regular expressions are too slow for my purpose. The problem is not the processing of highlightBlock but the fact that QSyntaxHighlighter isn't interruptable internally. It always runs through ALL lines on the first call which is triggered by the modification of the QTextDocument (that is internally directly connected to the private class).
-
@aha_1980 Thanks for the link. I filed a suggestion: https://bugreports.qt.io/browse/QTBUG-74701
-
Solution:
I created my own highlighter. It marks one or several disjoint regions as "dirty". Then it highlights an amount of lines shrinks the dirty region and if dirty regions remain it shoots a timer to resume highlighting later. If any change occurs on the document while highlighting it just adds a dirty region and keeps highlighting alive.