QtQTextEdit语法⾼亮
QTextEdit 语法⾼亮
Qt 提供了 QSyntaxHighlighter 类 来实现语法⾼亮,Qt 提供了⼀个例⼦ 讲解继承 QSyntaxHighlighter 来⾃定义⾃⼰的语法⾼亮格式
highlighter.h
#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H
#include <QSyntaxHighlighter>
高尔夫品牌#include <QTextCharFormat>
#include <QRegularExpression>
QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE
//! [0]
class Highlighter : public QSyntaxHighlighter //继承QSyntaxHightliaghter
对某人友好{
Q_OBJECT
public:
Highlighter(QTextDocument *parent = 0);//构造函数传⼀个 QTextDocument 对象给⽗类
protected:
void highlightBlock(const QString &text) override;//重写⽗类这个函数⾃动调⽤
private:
阳朔自驾游攻略struct HighlightingRule//语法规则结构体,包含正则表达式模式串和匹配的样式
{
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;//规则的集合,可以定义多个⾼亮规则
QRegularExpression commentStartExpression; //注释的⾼亮,使⽤highliangBlock()匹配,下⽂提到
QRegularExpression commentEndExpression;
QTextCharFormat keywordFormat;//⾼亮样式,关键词,⼀下顾名思义
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat multiLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat functionFormat;
};
//! [0]
#endif // HIGHLIGHTER_H
highlighter.cpp
#include "highlighter.h"
//! [0]
Highlighter::Highlighter(QTextDocument *parent)
林徽因
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.tForeground(Qt::darkBlue);//设定关键词的⾼亮样式
keywordFormat.tFontWeight(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" << "\\bbool\\b";
foreach (const QString &pattern, keywordPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
//! [0] //! [1]
}
//! [1]
//! [2]
classFormat.tFontWeight(QFont::Bold);//添加Qt的类到⾼亮规则中
classFormat.tForeground(Qt::darkMagenta);
rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b");
rule.format = classFormat;
highlightingRules.append(rule);
//! [2]
//! [3]
singleLineCommentFormat.tForeground(Qt::red);//单⾏注释
rule.pattern = QRegularExpression("//[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.tForeground(Qt::red);//多⾏注释,只设定了样式,具体匹配在highlightBlock中设置//! [3]
//! [4]醋可以洗脸吗
生活的馈赠quotationFormat.tForeground(Qt::darkGreen);//字符串
rule.pattern = QRegularExpression("\".*\"");
rule.format = quotationFormat;
世界杯出线highlightingRules.append(rule);
//! [4]
//! [5]
functionFormat.tFontItalic(true);//函数
functionFormat.tForeground(Qt::blue);
rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()");
rule.format = functionFormat;
highlightingRules.append(rule);
//! [5]
/
/! [6]
commentStartExpression = QRegularExpression("/\\*");//多⾏注释的匹配正则表达式
commentEndExpression = QRegularExpression("\\*/");
}
//! [6]
//! [7]
void Highlighter::highlightBlock(const QString &text)//应⽤⾼亮规则,也⽤于区块的⾼亮,⽐如多⾏注释
{
foreach (const HighlightingRule &rule, highlightingRules) {//⽂本采⽤⾼亮规则
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = ();
tFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
//! [7] //! [8]
tCurrentBlockState(0); //以下是多⾏注释的匹配
//! [8]
//! [9]
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(commentStartExpression);
/
/! [9] //! [10]
while (startIndex >= 0) {
//! [10] //! [11]
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
int endIndex = match.capturedStart();
int commentLength = 0;
if (endIndex == -1) {
tCurrentBlockState(1);
动物展览commentLength = text.length() - startIndex;
} el {
commentLength = endIndex - startIndex
+ match.capturedLength();
}
tFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
}
}
//! [11]
使⽤很简单
QTextEdit* pEdit = new QTextEdit();
Highlighter * h = new Highlighter(pEdit->document());//传⼀个QTextDocument
98年菜鸡⼀枚,请⼤佬们多多关照!