summaryrefslogtreecommitdiffstats
path: root/src/gui/doc/snippets/code
diff options
context:
space:
mode:
authorAaron Linville <aaron@linville.org>2017-02-02 22:13:32 -0500
committerAaron Linville <aaron@linville.org>2017-02-15 10:29:21 +0000
commita05116e6f1f87027eb90a24da4895577f596f3c9 (patch)
tree54481b9894f07af81a5a43c73f7a177339fc7f06 /src/gui/doc/snippets/code
parentaebf66d242614334962a1d339ac6c168d9e3f9e6 (diff)
Update QSyntaxHighlighter docs to use QRegularExpression
Update the QSyntaxHighlighter examples to use the new QRegularExpression class in place of QRegExp. Fix typos. Remove duplicated snippet. Replace lengthy section of duplicate text in highlight(..) with a note to see the detailed description. Task-number: QTBUG-58494 Change-Id: Id8d94bddbed52e6e52feac107f6fc84e2fe4518a Reviewed-by: Samuel Gaist <samuel.gaist@edeltech.ch> Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
Diffstat (limited to 'src/gui/doc/snippets/code')
-rw-r--r--src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp43
1 files changed, 12 insertions, 31 deletions
diff --git a/src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp b/src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp
index da960088b7..bb27eb9612 100644
--- a/src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp
+++ b/src/gui/doc/snippets/code/src_gui_text_qsyntaxhighlighter.cpp
@@ -60,14 +60,13 @@ void MyHighlighter::highlightBlock(const QString &text)
QTextCharFormat myClassFormat;
myClassFormat.setFontWeight(QFont::Bold);
myClassFormat.setForeground(Qt::darkMagenta);
- QString pattern = "\\bMy[A-Za-z]+\\b";
- QRegExp expression(pattern);
- int index = text.indexOf(expression);
- while (index >= 0) {
- int length = expression.matchedLength();
- setFormat(index, length, myClassFormat);
- index = text.indexOf(expression, index + length);
+ QRegularExpression expression("\\bMy[A-Za-z]+\\b");
+ QRegularExpressionMatchIterator i = expression.globalMatch(text);
+ while (i.hasNext())
+ {
+ QRegularExpressionMatch match = i.next();
+ setFormat(match.capturedStart(), match.capturedLength(), myClassFormat);
}
}
//! [1]
@@ -77,8 +76,8 @@ void MyHighlighter::highlightBlock(const QString &text)
QTextCharFormat multiLineCommentFormat;
multiLineCommentFormat.setForeground(Qt::red);
-QRegExp startExpression("/\\*");
-QRegExp endExpression("\\*/");
+QRegularExpression startExpression("/\\*");
+QRegularExpression endExpression("\\*/");
setCurrentBlockState(0);
@@ -87,14 +86,15 @@ if (previousBlockState() != 1)
startIndex = text.indexOf(startExpression);
while (startIndex >= 0) {
- int endIndex = text.indexOf(endExpression, startIndex);
+ QRegularExpressionMatch endMatch;
+ int endIndex = text.indexOf(endExpression, startIndex, &endMatch);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
- + endExpression.matchedLength();
+ + endMatch.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(startExpression,
@@ -104,25 +104,6 @@ while (startIndex >= 0) {
//! [3]
-void MyHighlighter::highlightBlock(const QString &text)
-{
- QTextCharFormat myClassFormat;
- myClassFormat.setFontWeight(QFont::Bold);
- myClassFormat.setForeground(Qt::darkMagenta);
- QString pattern = "\\bMy[A-Za-z]+\\b";
-
- QRegExp expression(pattern);
- int index = text.indexOf(expression);
- while (index >= 0) {
- int length = expression.matchedLength();
- setFormat(index, length, myClassFormat);
- index = text.indexOf(expression, index + length);
- }
- }
-//! [3]
-
-
-//! [4]
struct ParenthesisInfo
{
QChar char;
@@ -133,4 +114,4 @@ struct BlockData : public QTextBlockUserData
{
QVector<ParenthesisInfo> parentheses;
};
-//! [4]
+//! [3]