PDA

View Full Version : Forcing a QTextEdit to highlight



high_flyer
25th November 2012, 03:01
Hi,

I wrote a small RegExp tool and I have the following problem:
The tool has two text fields - one where the user can type the regular expression (QPlainTextEdit), and another which contains a test text and on which the current regular expression is being executed (QTextEdit).
As the user is typing the expression, I want to highlight the matching text in the test text QTextEdit.
To achieve this I used QSyntaxHighlighter.
It all works nicely, except, that the QSyntaxHighlighter::highlightBlock() is called by the document when it was changed.
Since I am not changing the test text but the RegExp, the highlighting doesn't get triggered unless I change the test text.

My question is:
Does any one know if there is a way to force QTextEdit to emit textChanged() or any similar notification that will trigger the highlighting without subclassing QTextEdit?
Just out of curiosity.

Thanks.

wysota
25th November 2012, 06:37
There is another approach you can take. Make use of QTextEdit::setExtraSelections(). It should be much simpler to use than the syntax highlighter.

high_flyer
25th November 2012, 23:45
Thanks for the tip Witek.
I had a look at it, and implemented it too (just to see what would be the difference), and I must say I don't find it any easier, nor in any way better than using the syntax highlighter, on so many levels.
The QSyntaxHighlighter is by far a cleaner design (specially from encapsulation and open-close perspectives), and more efficient - at least in the sense of the code it forces you to implement.
I'll try sub classing QTextEdit and allow a spontaneous (extern) refresh of the highlighter, and see if it works, if not, I'll fall back to the setExtraSelections() solution.

Cheers.