Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
11
namespace
src_gui_text_qsyntaxhighlighter
{
12
struct
MyHighlighter
:
public
QSyntaxHighlighter
13
{
14
explicit
MyHighlighter
(QTextDocument *document) :
QSyntaxHighlighter
(
document
) { Q_UNUSED(document); }
15
16
void
highlightBlock
(
const
QString &text);
17
void
wrapper
();
18
19
QString
text
;
20
};
21
22
//! [0]
23
QTextEdit
*
editor
=
new
QTextEdit
;
24
MyHighlighter
*
highlighter
=
new
MyHighlighter
(editor->document());
25
//! [0]
26
27
28
//! [1]
29
void
MyHighlighter
::
highlightBlock
(
const
QString &text)
30
{
31
QTextCharFormat myClassFormat;
32
myClassFormat.setFontWeight(QFont::Bold);
33
myClassFormat.setForeground(Qt::darkMagenta);
34
35
QRegularExpression expression(
"\\bMy[A-Za-z]+\\b"
);
36
QRegularExpressionMatchIterator i = expression.globalMatch(text);
37
while
(i.hasNext()) {
38
QRegularExpressionMatch match = i.next();
39
setFormat(match.capturedStart(), match.capturedLength(), myClassFormat);
40
}
41
}
42
//! [1]
43
44
void
MyHighlighter
::
wrapper
() {
45
//! [2]
46
QTextCharFormat multiLineCommentFormat;
47
multiLineCommentFormat.setForeground(Qt::red);
48
49
QRegularExpression startExpression(
"/\\*"
);
50
QRegularExpression endExpression(
"\\*/"
);
51
52
setCurrentBlockState(0);
53
54
int
startIndex = 0;
55
if
(previousBlockState() != 1)
56
startIndex = text.indexOf(startExpression);
57
58
while
(startIndex >= 0) {
59
QRegularExpressionMatch endMatch;
60
int
endIndex = text.indexOf(endExpression, startIndex, &endMatch);
61
int
commentLength;
62
if
(endIndex == -1) {
63
setCurrentBlockState(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
}
73
//! [2]
74
}
// MyHighlighter::wrapper
75
76
77
//! [3]
78
struct
ParenthesisInfo
79
{
80
QChar
character
;
81
int
position
;
82
};
83
84
struct
BlockData
:
public
QTextBlockUserData
85
{
86
QList
<
ParenthesisInfo
>
parentheses
;
87
};
88
//! [3]
89
90
}
// src_gui_text_qsyntaxhighlighter
src_gui_text_qsyntaxhighlighter
Definition
src_gui_text_qsyntaxhighlighter.cpp:11
src_gui_text_qsyntaxhighlighter::highlighter
MyHighlighter * highlighter
Definition
src_gui_text_qsyntaxhighlighter.cpp:24
src_gui_text_qsyntaxhighlighter::editor
QTextEdit * editor
[0]
Definition
src_gui_text_qsyntaxhighlighter.cpp:23
src_gui_text_qsyntaxhighlighter::BlockData
Definition
src_gui_text_qsyntaxhighlighter.cpp:85
src_gui_text_qsyntaxhighlighter::BlockData::parentheses
QList< ParenthesisInfo > parentheses
Definition
src_gui_text_qsyntaxhighlighter.cpp:86
src_gui_text_qsyntaxhighlighter::MyHighlighter
Definition
src_gui_text_qsyntaxhighlighter.cpp:13
src_gui_text_qsyntaxhighlighter::MyHighlighter::highlightBlock
void highlightBlock(const QString &text)
[0]
Definition
src_gui_text_qsyntaxhighlighter.cpp:29
src_gui_text_qsyntaxhighlighter::MyHighlighter::wrapper
void wrapper()
[1]
Definition
src_gui_text_qsyntaxhighlighter.cpp:44
src_gui_text_qsyntaxhighlighter::MyHighlighter::text
QString text
Definition
src_gui_text_qsyntaxhighlighter.cpp:19
src_gui_text_qsyntaxhighlighter::MyHighlighter::MyHighlighter
MyHighlighter(QTextDocument *document)
Definition
src_gui_text_qsyntaxhighlighter.cpp:14
src_gui_text_qsyntaxhighlighter::ParenthesisInfo
[3]
Definition
src_gui_text_qsyntaxhighlighter.cpp:79
src_gui_text_qsyntaxhighlighter::ParenthesisInfo::character
QChar character
Definition
src_gui_text_qsyntaxhighlighter.cpp:80
src_gui_text_qsyntaxhighlighter::ParenthesisInfo::position
int position
Definition
src_gui_text_qsyntaxhighlighter.cpp:81
qtbase
src
gui
doc
snippets
code
src_gui_text_qsyntaxhighlighter.cpp
Generated on
for Qt by
1.18.0