How to highlight a string of text within a QTextEdit
Asked Answered
V

1

9

I'm a student programmer currently developing an application for work using Qt4. I am building an equation editor and I'm having issues attempting to highlight a string within my QTextEdit field. I have a function that parses through the QTextEdit string and returns an a start and end integer of where an error is located. My original strategy was to use HTML tags at these two points to highlight the error. Unfortunately there appears to be an issue with html tagging and the equation syntax.

What I think I need is a strategy that relies on Qt's library to set a background color between these two indices. I began looking a QSyntaxHighlighter; however I think that this is more for highlighting using a predefined set of laws and not for just grabbing up anything between a & b and setting the background color. If I can use syntax highlighter please provide me with and example or reference as I have already read through the documentation and didn't find anything.

Thanks for any help in advance!

P.S. Just to emphasize on the html compatibility issues; html becomes problematic due to multiple < and > signs used.

Vivianviviana answered 14/2, 2013 at 22:46 Comment(0)
Y
18

You can use QTextCursor and QTextCharFormat for it:

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);
Yoshi answered 15/2, 2013 at 6:18 Comment(5)
This is what I eventually ended up turning to.. The problem with this answer is that it appears I cannot set the text back to the default color afterwards. What I ended up doing is scratching the background color change and setting my particular segment of error prone equation to simply be the selected text until the mouse clicked to a location otherwise. I don't highlight problems in red obviously, however this solution did what I needed; highlight the text I needed for a more intuitive means of equation checking.Vivianviviana
The same way you can set default background color. You just need to save the text positions you have selected before.Yoshi
Unfortunately that's not feasible due to the reliance on a trigger emitted from the text edit field. Once the user clicks back into the box I would like for the highlighting to disappear. Resetting the cursor every time this signal is emitted causes the user to lose their ability to right click and select text. Its ok if you don't know a way for that; I just figured I'd ask.Vivianviviana
Thankyou hank! I found that to preserve any existing text format like font size, italic etc, you can get the current format instead of creating a completely new one. QTextCharFormat fmt = cursor.charFormat(); fmt.setBackground(Qt::yellow); cursor.setCharFormat(fmt);Figureground
How can I calculate the end when searching with regular expressions?Tracheo

© 2022 - 2024 — McMap. All rights reserved.