实现文本的代码高亮。
本来初衷以为应该是自己用正则实现 不过发现有一个封装好的专门用于代码高亮的的类
QSyntaxHighlighter 通过其子类函数实现自定义代码高亮
构造函数可以用接受一个QTextDoucument*函数
explicit QSyntaxHighlighter(QObject *parent);
explicit QSyntaxHighlighter(QTextDocument *parent);
virtual ~QSyntaxHighlighter();
用于定义高亮代码块的函数
在QSynntaxHighlighter 定义了一个函数 void MyHighlighter::highlightBlock(constQString &text)
要实现自定义代码高亮规则 需要在子类中重写这个函数
h
#ifndef MAJOR_HIGHLIGHTER_H
#define MAJOR_HIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
//语法高亮---QSyntaxHighlighter
class major_highlighter : public QSyntaxHighlighter //定义一个类继承自QSyntaxHightliaghter
{
Q_OBJECT //Qt宏定义,使用Qt元编程
public:
major_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;
};
#endif
cpp
#include "major_highlighter.h"
major_highlighter::major_highlighter(QTextDocument *parent) //构造函数,主要是对词语的高亮
: QSyntaxHighlighter(parent)
{
HighlightingRule rule; //高亮规则
keywordFormat.setForeground(Qt::darkBlue); //设定关键词的高亮样式
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns; //关键词集合,关键都以正则表达式表示
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 major_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);
}
}
在mainwindow初始化中加入
QFont font;
font.setFamily("Courier");
font.setFixedPitch(true);
font.setPointSize(10);
ui->textEdit->setFont(font);
major_highlighter *highter = new major_highlighter(ui->textEdit->document());
以上major_highlighter实现参考自http://www.cnblogs.com/lenxvp/p/5475931.html 带完善