Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Questions on how to use QSyntaxHighlighter
Forum Updated to NodeBB v4.3 + New Features

Questions on how to use QSyntaxHighlighter

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ritchan
    wrote on last edited by
    #1

    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 http://harmattan-dev.nokia.com/docs/library/html/qt4/qsyntaxhighlighter.html) 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)
    {

    };
    Highlighter::~Highlighter(){};
    void Highlighter::highlightBlock(const QString &text){
    QTextCharFormat foundFormat;
    foundFormat.setFontWeight(QFont::Bold);
    foundFormat.setForeground(Qt::darkMagenta);

    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;
    }
    };@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ritchan
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          ritchan
          wrote on last edited by
          #4

          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){
          textOutput->moveCursor(QTextCursor::Start);
          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;
          foundFormat.setFontWeight(QFont::Bold);
          foundFormat.setForeground(Qt::darkMagenta);

          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);
          }
          };@

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved