Doc: updated QSyntaxHighlighter documentation and use QRegularExpression
The documentation of QSyntaxHighlighter still uses the deprecated QRegExp class. This patch updates the code samples as well as cleanup some typos. Change-Id: I87b525fddb560b7c5bb38f96d9aaceadb594f76c Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
This commit is contained in:
parent
feaaca456b
commit
98e0cb0a28
@ -29,8 +29,8 @@
|
||||
\example richtext/syntaxhighlighter
|
||||
\title Syntax Highlighter Example
|
||||
\ingroup examples-richtext
|
||||
\brief The Syntax Highligher example shows how to perform
|
||||
simple syntax highlighing.
|
||||
\brief The Syntax Highlighter example shows how to perform
|
||||
simple syntax highlighting.
|
||||
|
||||
\brief The Syntax Highlighter example shows how to perform simple syntax
|
||||
highlighting by subclassing the QSyntaxHighlighter class.
|
||||
@ -64,8 +64,9 @@
|
||||
and define your own highlighting rules.
|
||||
|
||||
We have chosen to store our highlighting rules using a private
|
||||
struct: A rule consists of a QRegExp pattern and a QTextCharFormat
|
||||
instance. The various rules are then stored using a QVector.
|
||||
struct: A rule consists of a QRegularExpression pattern and a
|
||||
QTextCharFormat instance. The various rules are then stored using a
|
||||
QVector.
|
||||
|
||||
The QTextCharFormat class provides formatting information for
|
||||
characters in a QTextDocument specifying the visual properties of
|
||||
@ -78,7 +79,7 @@
|
||||
|
||||
When subclassing the QSyntaxHighlighter class you must pass the
|
||||
parent parameter to the base class constructor. The parent is the
|
||||
text document upon which the syntax highligning will be
|
||||
text document upon which the syntax highlighting will be
|
||||
applied. In this example, we have also chosen to define our
|
||||
highlighting rules in the constructor:
|
||||
|
||||
@ -140,9 +141,9 @@
|
||||
HighlightingRule object) we search for the pattern in the given
|
||||
text block using the QString::indexOf() function. When the first
|
||||
occurrence of the pattern is found, we use the
|
||||
QRegExp::matchedLength() function to determine the string that
|
||||
will be formatted. QRegExp::matchedLength() returns the length of
|
||||
the last matched string, or -1 if there was no match.
|
||||
QRegularExpressionMatch::capturedLength() function to determine the string
|
||||
that will be formatted. QRegularExpressionMatch::capturedLength() returns
|
||||
the length of the last matched string, or 0 if there was no match.
|
||||
|
||||
To perform the actual formatting the QSyntaxHighlighter class
|
||||
provides the \l {QSyntaxHighlighter::setFormat()}{setFormat()}
|
||||
|
@ -68,9 +68,9 @@ Highlighter::Highlighter(QTextDocument *parent)
|
||||
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
|
||||
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
|
||||
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
|
||||
<< "\\bvoid\\b" << "\\bvolatile\\b";
|
||||
<< "\\bvoid\\b" << "\\bvolatile\\b" << "\\bbool\\b";
|
||||
foreach (const QString &pattern, keywordPatterns) {
|
||||
rule.pattern = QRegExp(pattern);
|
||||
rule.pattern = QRegularExpression(pattern);
|
||||
rule.format = keywordFormat;
|
||||
highlightingRules.append(rule);
|
||||
//! [0] //! [1]
|
||||
@ -80,14 +80,14 @@ Highlighter::Highlighter(QTextDocument *parent)
|
||||
//! [2]
|
||||
classFormat.setFontWeight(QFont::Bold);
|
||||
classFormat.setForeground(Qt::darkMagenta);
|
||||
rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
|
||||
rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b");
|
||||
rule.format = classFormat;
|
||||
highlightingRules.append(rule);
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
singleLineCommentFormat.setForeground(Qt::red);
|
||||
rule.pattern = QRegExp("//[^\n]*");
|
||||
rule.pattern = QRegularExpression("//[^\n]*");
|
||||
rule.format = singleLineCommentFormat;
|
||||
highlightingRules.append(rule);
|
||||
|
||||
@ -96,7 +96,7 @@ Highlighter::Highlighter(QTextDocument *parent)
|
||||
|
||||
//! [4]
|
||||
quotationFormat.setForeground(Qt::darkGreen);
|
||||
rule.pattern = QRegExp("\".*\"");
|
||||
rule.pattern = QRegularExpression("\".*\"");
|
||||
rule.format = quotationFormat;
|
||||
highlightingRules.append(rule);
|
||||
//! [4]
|
||||
@ -104,14 +104,14 @@ Highlighter::Highlighter(QTextDocument *parent)
|
||||
//! [5]
|
||||
functionFormat.setFontItalic(true);
|
||||
functionFormat.setForeground(Qt::blue);
|
||||
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
|
||||
rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()");
|
||||
rule.format = functionFormat;
|
||||
highlightingRules.append(rule);
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
commentStartExpression = QRegExp("/\\*");
|
||||
commentEndExpression = QRegExp("\\*/");
|
||||
commentStartExpression = QRegularExpression("/\\*");
|
||||
commentEndExpression = QRegularExpression("\\*/");
|
||||
}
|
||||
//! [6]
|
||||
|
||||
@ -119,12 +119,10 @@ Highlighter::Highlighter(QTextDocument *parent)
|
||||
void Highlighter::highlightBlock(const QString &text)
|
||||
{
|
||||
foreach (const HighlightingRule &rule, highlightingRules) {
|
||||
QRegExp expression(rule.pattern);
|
||||
int index = expression.indexIn(text);
|
||||
while (index >= 0) {
|
||||
int length = expression.matchedLength();
|
||||
setFormat(index, length, rule.format);
|
||||
index = expression.indexIn(text, index + length);
|
||||
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
|
||||
while (matchIterator.hasNext()) {
|
||||
QRegularExpressionMatch match = matchIterator.next();
|
||||
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
|
||||
}
|
||||
}
|
||||
//! [7] //! [8]
|
||||
@ -134,22 +132,23 @@ void Highlighter::highlightBlock(const QString &text)
|
||||
//! [9]
|
||||
int startIndex = 0;
|
||||
if (previousBlockState() != 1)
|
||||
startIndex = commentStartExpression.indexIn(text);
|
||||
startIndex = text.indexOf(commentStartExpression);
|
||||
|
||||
//! [9] //! [10]
|
||||
while (startIndex >= 0) {
|
||||
//! [10] //! [11]
|
||||
int endIndex = commentEndExpression.indexIn(text, startIndex);
|
||||
int commentLength;
|
||||
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
|
||||
int endIndex = match.capturedStart();
|
||||
int commentLength = 0;
|
||||
if (endIndex == -1) {
|
||||
setCurrentBlockState(1);
|
||||
commentLength = text.length() - startIndex;
|
||||
} else {
|
||||
commentLength = endIndex - startIndex
|
||||
+ commentEndExpression.matchedLength();
|
||||
+ match.capturedLength();
|
||||
}
|
||||
setFormat(startIndex, commentLength, multiLineCommentFormat);
|
||||
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
|
||||
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
|
||||
}
|
||||
}
|
||||
//! [11]
|
||||
|
@ -53,6 +53,7 @@
|
||||
|
||||
#include <QSyntaxHighlighter>
|
||||
#include <QTextCharFormat>
|
||||
#include <QRegularExpression>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTextDocument;
|
||||
@ -72,13 +73,13 @@ protected:
|
||||
private:
|
||||
struct HighlightingRule
|
||||
{
|
||||
QRegExp pattern;
|
||||
QRegularExpression pattern;
|
||||
QTextCharFormat format;
|
||||
};
|
||||
QVector<HighlightingRule> highlightingRules;
|
||||
|
||||
QRegExp commentStartExpression;
|
||||
QRegExp commentEndExpression;
|
||||
QRegularExpression commentStartExpression;
|
||||
QRegularExpression commentEndExpression;
|
||||
|
||||
QTextCharFormat keywordFormat;
|
||||
QTextCharFormat classFormat;
|
||||
|
Loading…
x
Reference in New Issue
Block a user