-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyHighlighter.cpp
33 lines (28 loc) · 1013 Bytes
/
MyHighlighter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// Created by Administrator on 24-7-21.
//
#include "MyHighlighter.h"
MyHighlighter::MyHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) {
addNormalTextFormat();
}
void MyHighlighter::highlightBlock(const QString &text) {
for (const HighlightRule &rule: highlightRules) {
QRegularExpression re(rule.pattern);
QRegularExpressionMatchIterator remi = re.globalMatch(text);
while (remi.hasNext()) {
QRegularExpressionMatch rem = remi.next();
int start = (int) rem.capturedStart();
int count = (int) rem.capturedLength();
setFormat(start, count, rule.format);
}
}
}
void MyHighlighter::addNormalTextFormat() {
HighlightRule rule;
rule.pattern = QRegularExpression("[a-z0-9A-Z]+");
QTextCharFormat normalFormat;
normalFormat.setFont(QFont(mFontFamily, mFontSize));
normalFormat.setForeground(QColor(0, 100, 100));
rule.format = normalFormat;
highlightRules.append(rule);
}