Important: Please read the Qt Code of Conduct -

Questions on how to use QSyntaxHighlighter

  • Hi, I'm trying to use QSyntaxHighlighter in my code to highlight relevant parts of some program output in a QTextEdit. So I derived a class from QSyntaxHighlighter and wrote highlightBlock(). This
    @ Highlighter *highlight = new Highlighter(textOutput->document());
    will be called whenever the user changes something in the search box (a QLineEdit) using QLineEdit's textEdited() signal.

    I'm not sure on a few things though. In Line 12 of my code below (I didn't think it was necessary to add anything to the constructor since it should be based on QSyntaxHighlighter), I make a QRegExp from a QString. This QString will just be a plaintext string that will be provided by a QLineEdit. I just want a simple case sensitive search. Would QRegExp [removed]pattern) give me what I want? What kind of regular expression will it generate from this QString?

    Also, when I try running this program, I get an infinite loop, where the index is always 0. Index is never decremented, so I don't see how this piece of code (which I copied from is supposed to work at all. Also, does index =0 mean it's pointing to the start of the QTextDocument, or if it's pointing to the end? Is it the cursor position? Or does each index increment denote a found instance of the search string?

    @Highlighter::Highlighter(QTextDocument *parent):QSyntaxHighlighter(parent)

    void Highlighter::highlightBlock(const QString &text){
    QTextCharFormat foundFormat;

    QString pattern = text;
    QRegExp [removed]pattern);
    int index = text.indexOf(expression);
    while (index >= 0) {
    int length = expression.matchedLength();
    setFormat(index, length, foundFormat);
    index = text.indexOf(expression, index + length);
    qDebug() << index;

  • Lifetime Qt Champion


    Your code doesn't make sense. You set pattern to text, then (maybe) QRegExp expression to pattern and you are trying to search expression in text which has the same content as pattern which contains text. Thus index will always be 0

  • Well from my experiments in a few text editors, a regex pattern which is exactly the same as the string to be found should still match. In other words, it should still work much the same way as a find() function.

  • I noticed something - my reimplemented highlightBlock(QString text) is not receiving a string to search in its arguments. The variables text, pattern, and thus expression are empty!

    The Qt Documentation says that highlightBlock() is called automatically by something else. The only part of my code which has anything to do with my reimplemented QSyntaxHighlighter is this:
    @void DcmDumpWindow::find(QString findWhat){
    qDebug() << textOutput->find(findWhat,QTextDocument::FindCaseSensitively);
    Highlighter *highlight = new Highlighter(textOutput->document());

    This is a slot that is called automatically by a QLineEdit's textEdited() signal. So it seems that the QSyntaxHighlighter constructor automatically calls highlightBlock(). However, there is no way to give highlightBlock the string to search for. Am I misunderstanding some concept here?

    EDIT: Since then I've noticed that the QRegExp thing is unnecessary. I just want to search a string, no fancy regexp matching here. So now my code looks like this, but there's still no way to get the string I want to highlight into the variable text:
    @void Highlighter::highlightBlock(const QString &text){
    QTextCharFormat foundFormat;

    QString pattern = text;
    qDebug() << "text: " << text;
    qDebug() << "pattern: " << pattern;
    int index = text.indexOf(pattern);
    while (index >= 0) {
    int length = pattern.length();
    setFormat(index, length, foundFormat);
    index = text.indexOf(pattern, index + length);

  • Lifetime Qt Champion

    Seems my answer has been lost... So here we go again:
    I think I found out what the problem is:
    You should not call highlightBlock, it is done for you when the underlying document changes. So what you should do to achieve what you want is:

    • add a slot to your Highlighter class that sets a member QString variable to the content of your line edit.
    • call rehighlight()
    • use your member variable as pattern in highlightBlock

Log in to reply