Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
83
85{
86 QList<ParenthesisInfo> parentheses;
87};
89
90} // src_gui_text_qsyntaxhighlighter
\inmodule QtCore
@ Bold
Definition qfont.h:70
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
\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:129
qsizetype indexOf(QLatin1StringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4517
qsizetype length() const noexcept
Returns the number of characters in this string.
Definition qstring.h:191
\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:45
@ red
Definition qnamespace.h:35
#define Q_UNUSED(x)
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)