How to highlight a line of text in a QTextEdit

I am a software programmer who is currently developing an application for working with Qt4. I am creating an equation editor and I am having trouble trying to highlight a row in the QTextEdit field . I have a function that parses a QTextEdit string and returns the integer of the beginning and end where the error is. My initial strategy was to use HTML tags at these two points to highlight the error. Unfortunately, there is a problem with html tags and equation syntax.

It seems to me that I need a strategy that relies on the Qt library to set the background color between these two indexes. I started looking for QSyntaxHighlighter ; however, I think this is more for highlighting using a predefined set of laws, rather than just grabbing something between a and b and setting the background color. If I can use syntax highlighting, please provide me with an example or link as well, since I already read the documentation and found nothing.

Thanks for any help in advance!

PS Just highlight html compatibility issues; html becomes problematic due to the many <and> characters used.

+5
source share
1 answer

QTextCursor QTextCharFormat :

QTextEdit *edit = new QTextEdit;
...
int begin = ...
int end = ...
...

QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);

QTextCursor cursor(edit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(fmt);
+10

All Articles