//语法高亮---QSyntaxHighlighter
//highlighter.h
class Highlighter : public QSyntaxHighlighter //定义一个类继承自QSyntaxHightliaghter
{
Q_OBJECT //Qt宏定义
public:
Highlighter(QTextDocument *parent = 0); //构造函数,传递一个QTextDocument对象给其父类
protected:
void highlightBlock(const QString &text) Q_DECL_OVERRIDE; //块高亮使用的函数,自动调用
private:
struct HighlightingRule //语法规则结构体,包含正则表达式模式串和匹配的样式
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules; //规则的集合,可以定义多个高亮规则
QRegExp commentStartExpression; //注释的高亮,使用highliangBlock()匹配,下文提到
QRegExp commentEndExpression;
QTextCharFormat keywordFormat; //高亮样式,关键词,一下顾名思义
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat multiLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat functionFormat;
};
//highlighter.c
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent) //构造函数,主要是对词语的高亮
: QSyntaxHighlighter(parent)
{
HighlightingRule rule; //高亮规则
keywordFormat.setForeground(Qt::darkBlue); //设定关键词的高亮样式
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns; //关键词集合,关键都以正则表达式表示
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
<< "\\bvoid\\b" << "\\bvolatile\\b";
foreach (const QString &pattern, keywordPatterns) { //添加各个关键词到高亮规则中
rule.pattern = QRegExp(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
classFormat.setFontWeight(QFont::Bold); //添加Qt的类到高亮规则中
classFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
rule.format = classFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(Qt::red); //单行注释
rule.pattern = QRegExp("//[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::red); //多行注释,只设定了样式,具体匹配在highlightBlock中设置
quotationFormat.setForeground(Qt::darkGreen); //字符串
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);
functionFormat.setFontItalic(true); //函数
functionFormat.setForeground(Qt::blue);
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
rule.format = functionFormat;
highlightingRules.append(rule);
commentStartExpression = QRegExp("/\\*"); //多行注释的匹配正则表达式
commentEndExpression = QRegExp("\\*/");
}
void Highlighter::highlightBlock(const QString &text) //应用高亮规则,也用于区块的高亮,比如多行注释
{
foreach (const HighlightingRule &rule, highlightingRules) { //文本采用高亮规则
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0); //以下是多行注释的匹配
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}
}
//used in editor
void MainWindow::setupEditor()
{
QFont font;
font.setFamily("Courier");
font.setFixedPitch(true);
font.setPointSize(10);
editor = new QTextEdit;
editor->setFont(font);
highlighter = new Highlighter(editor->document()); //调用方法,新建对象并传递document
QFile file("mainwindow.h");
if (file.open(QFile::ReadOnly | QFile::Text))
editor->setPlainText(file.readAll());
}