PDA

View Full Version : QTextEdit, Highlighting and textChanged()



onamatic
11th November 2008, 22:23
I have a QSyntaxHighlighter "installed" on a QTextEdit ( called textEdit).

on_textEdit_textChange() sets a "document dirty" flag whenever the user changes the contents of textEdit.

Works fine, BUT if I set the highlighter to highlight some text (in repsonse to user entered text in a lineEdit), it causes on_textEdit_textChange() to fire again thus setting my dirty flag although the text hasn't "really" changed.

I tried putting a flag up saying "don't set document dirty" before firing up the highlighter and resetting it afterwards. Unfortunately, I think the highlighting process runs asynchronously (QSyntaxHighlighter::rehighlight is a slot) so my "don't set document dirty" flag has come and gone well before the highlighting is actually done.

Any thoughts gratefully received!

caduel
11th November 2008, 22:31
maybe you have more luck with QTextDocument::contentsChanged()? (I fear you will not have that luck, but it is worth a try.)

onamatic
12th November 2008, 11:44
Thank you for that Caduel.

Using the document's isModified() function genuinely reflects the correct state and ignores any highlighting in the QTextEdit. For the moment, I just test that function in on_textEdit_textChanged and all is well.

I will rewrite it to use QTextDocument::contentsChanged() eventually; it's clearly the correct thing to do.

Thank you for an excellent solution.