一、描述
通过QSyntaxHighlighter高亮显示,通过QRegularExpression使用正则表达式匹配json格式。
读取json文件高亮显示json的键值,通过自定义语法高亮显示,子类化 QSyntaxHighlighter 并重新实现 highlightBlock(),此函数将在合适的时机自动被调用,这样QTextEdit显示出来的文本高亮颜色不是固定的,你可以编辑文本,颜色也会自动变化。
通过QRegularExpression 使用正则表达式匹配json格式。
匹配json中的键:
QRegularExpression regex("(\"\\w+\"):\\s*");
匹配json中的值:
QRegularExpression regex_1("(\".*?\")|(-?\\d+\\.?\\d*)|(true|false)|(null)");
二、自定义jsonHighlighter头文件
#pragma once
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>
class jsonHighlighter : public QSyntaxHighlighter
{
public:
jsonHighlighter(QTextDocument* parent = nullptr);
protected:
void highlightBlock(const QString& text) override;
private:
QTextCharFormat m_keyFormat;
QTextCharFormat m_stringFormat;
QTextCharFormat m_numberFormat;
QTextCharFormat m_booleanFormat;
QTextCharFormat m_nullFormat;
};
三、自定义jsonHighlighter源文件
#include "jsonHighlighter.h"
jsonHighlighter::jsonHighlighter(QTextDocument* parent)
: QSyntaxHighlighter(parent)
{
//自定义json各键值显示高亮颜色
m_keyFormat.setForeground(Qt::darkBlue);
QFont f;
f.setBold(true);
m_keyFormat.setFont(f);
m_stringFormat.setForeground(Qt::darkGreen);
m_numberFormat.setForeground(Qt::darkMagenta);
m_booleanFormat.setForeground(Qt::darkYellow);
m_nullFormat.setForeground(Qt::gray);
}
void jsonHighlighter::highlightBlock(const QString& text)
{
//匹配值
QRegularExpression regex_1("(\".*?\")|(-?\\d+\\.?\\d*)|(true|false)|(null)");
int index_1 = 0;
while (index_1 >= 0) {
QRegularExpressionMatch match = regex_1.match(text, index_1);
if (!match.hasMatch()) {
break;
}
int start = match.capturedStart(1);
int length = match.capturedLength(1);
if (match.capturedStart(1) != -1) { // String
setFormat(match.capturedStart(1), match.capturedLength(1), m_stringFormat);
}
else if (match.capturedStart(2) != -1) { // Number
setFormat(match.capturedStart(2), match.capturedLength(2), m_numberFormat);
}
else if (match.capturedStart(3) != -1) { // Boolean
setFormat(match.capturedStart(3), match.capturedLength(3), m_booleanFormat);
}
else if (match.capturedStart(4) != -1) { // Null
setFormat(match.capturedStart(4), match.capturedLength(4), m_nullFormat);
}
index_1 = match.capturedEnd();
}
//匹配键
QRegularExpression regex("(\"\\w+\"):\\s*");
int index = 0;
while (index >= 0) {
QRegularExpressionMatch match = regex.match(text, index);
if (!match.hasMatch()) {
break;
}
int start = match.capturedStart(1);
int length = match.capturedLength(1);
setFormat(start, length, m_keyFormat);
index = match.capturedEnd();
}
}
四、使用示例
void mainwindow::on_pushButton_openConfigFile_clicked()
{
QString jsonFileName = QFileDialog::getOpenFileName(this, "打开json文件", ".", "Json File(*.json)");
if (jsonFileName.isEmpty())
return;
QFile jsonFile(jsonFileName);
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&jsonFile);
QString jsonStr = in.readAll();
jsonFile.close();
QString jsonData = jsonStr.toUtf8();
ui.textEdit->setText(jsonData);
jsonHighlighter* highlighter = new jsonHighlighter(ui.textEdit->document());
}