Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Questions on how to use QSyntaxHighlighter

$
0
0
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;       } };

Viewing all articles
Browse latest Browse all 13965

Trending Articles