Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
src_gui_text_qsyntaxhighlighter.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3#include <QChar>
4#include <QList>
5#include <QRegularExpression>
6#include <QSyntaxHighlighter>
7#include <QTextBlockUserData>
8#include <QTextEdit>
9#include <QTextObject>
10
13{
15
16 void highlightBlock(const QString &text);
17 void wrapper();
18
20};
21
26
27
30{
31 QTextCharFormat myClassFormat;
32 myClassFormat.setFontWeight(QFont::Bold);
33 myClassFormat.setForeground(Qt::darkMagenta);
34
35 QRegularExpression expression("\\bMy[A-Za-z]+\\b");
37 while (i.hasNext()) {
39 setFormat(match.capturedStart(), match.capturedLength(), myClassFormat);
40 }
41}
43
46QTextCharFormat multiLineCommentFormat;
47multiLineCommentFormat.setForeground(Qt::red);
48
49QRegularExpression startExpression("/\\*");
50QRegularExpression endExpression("\\*/");
51
53
54int startIndex = 0;
55if (previousBlockState() != 1)
56 startIndex = text.indexOf(startExpression);
57
58while (startIndex >= 0) {
60 int endIndex = text.indexOf(endExpression, startIndex, &endMatch);
61 int commentLength;
62 if (endIndex == -1) {
64 commentLength = text.length() - startIndex;
65 } else {
66 commentLength = endIndex - startIndex
67 + endMatch.capturedLength();
68 }
69 setFormat(startIndex, commentLength, multiLineCommentFormat);
70 startIndex = text.indexOf(startExpression,
71 startIndex + commentLength);
72}
74} // MyHighlighter::wrapper
75
76
79{
82};
83
85{
87};
89
90} // src_gui_text_qsyntaxhighlighter
\inmodule QtCore
Definition qchar.h:48
@ Bold
Definition qfont.h:67
Definition qlist.h:74
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
qsizetype capturedLength(int nth=0) const
Returns the length of the substring captured by the nth capturing group.
\inmodule QtCore \reentrant
QRegularExpressionMatchIterator globalMatch(const QString &subject, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
Attempts to perform a global match of the regular expression against the given subject string,...
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString static QString qsizetype indexOf(QChar c, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4420
qsizetype length() const
Returns the number of characters in this string.
Definition qstring.h:187
\reentrant \inmodule QtGui
void setCurrentBlockState(int newState)
Sets the state of the current text block to newState.
int previousBlockState() const
Returns the end state of the text block previous to the syntax highlighter's current block.
QTextDocument * document() const
Returns the QTextDocument on which this syntax highlighter is installed.
void setFormat(int start, int count, const QTextCharFormat &format)
This function is applied to the syntax highlighter's current text block (i.e.
void setFontWeight(int weight)
Sets the text format's font weight to weight.
\reentrant \inmodule QtGui
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
Definition qtextedit.h:27
QTextDocument * document
the underlying document of the text editor.
Definition qtextedit.h:51
void setForeground(const QBrush &brush)
Sets the foreground brush to the specified brush.
QString text
@ darkMagenta
Definition qnamespace.h:44
@ red
Definition qnamespace.h:34
#define Q_UNUSED(x)
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)