How to highlight a string of text within a QTextEdit

13,557

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);
Share:
13,557
Wylie Coyote SG.
Author by

Wylie Coyote SG.

SE Student

Updated on June 04, 2022

Comments

  • Wylie Coyote SG.
    Wylie Coyote SG. almost 2 years

    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.

  • Wylie Coyote SG.
    Wylie Coyote SG. about 11 years
    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.
  • hank
    hank about 11 years
    The same way you can set default background color. You just need to save the text positions you have selected before.
  • Wylie Coyote SG.
    Wylie Coyote SG. about 11 years
    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.
  • iforce2d
    iforce2d over 10 years
    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);
  • birgersp
    birgersp almost 9 years
    How can I calculate the end when searching with regular expressions?