-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmysyntaxhighlighter.cpp
More file actions
29 lines (26 loc) · 989 Bytes
/
Copy pathmysyntaxhighlighter.cpp
File metadata and controls
29 lines (26 loc) · 989 Bytes
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
#include "mysyntaxhighlighter.h"
#include "label.h"
#include "QDebug"
MySyntaxHighlighter::MySyntaxHighlighter(QString str1,QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
str = str1;
}
void MySyntaxHighlighter::highlightBlock(const QString &text) //高亮文本块
{
QTextCharFormat myFormat; //字符格式
myFormat.setFontWeight(QFont::Bold);
myFormat.setForeground(Qt::red);
QString pattern = "\\b"+str+"\\b"; //要匹配的字符,这里是“char”单词
QRegExp expression(pattern); //创建正则表达式
expression.setCaseSensitivity(Qt::CaseInsensitive);
int index = text.indexOf(expression); //从位置0开始匹配字符串
//如果匹配成功,那么返回值为字符串的起始位置,它大于或等于0
while (index >= 0)
{
int length = expression.matchedLength(); //要匹配字符串的长度
setFormat(index, length, myFormat); //对要匹配的字符串设置格式
index = text.indexOf(expression, index + length);
//从前面匹配到的字符串之后继续匹配
}
}