QtQTextEdit语法高亮

更新时间:2023-07-25 20:45:44 阅读: 评论:0

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年菜鸡⼀枚,请⼤佬们多多关照!

本文发布于:2023-07-25 20:45:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1096486.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:匹配   关键词   规则   语法   个例
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图