企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# :-: 给代码添加高亮 在文件上右键选择`add new...` ![图2](https://s1.ax1x.com/2020/04/22/JtQkkT.png) ![图3](https://s1.ax1x.com/2020/04/22/Jtl0M9.png) 将`class name`命名为`highlighter` 将`.pro`文件修改 ```css QT += widgets requires(qtConfig(filedialog)) HEADERS = highlighter.h \ mainwindow.h SOURCES = highlighter.cpp \ mainwindow.cpp \ main.cpp # install target.path = $$[QT_INSTALL_EXAMPLES]/widgets/richtext/syntaxhighlighter INSTALLS += target ``` 将`highlighter.h`的代码修改为 ```css #ifndef HIGHLIGHTER_H #define HIGHLIGHTER_H #include <QSyntaxHighlighter> #include <QTextCharFormat> #include <QRegularExpression> QT_BEGIN_NAMESPACE class QTextDocument; QT_END_NAMESPACE class Highlighter : public QSyntaxHighlighter { Q_OBJECT public: Highlighter(QTextDocument *parent = 0); protected: void highlightBlock(const QString &text) override; private: struct HighlightingRule { QRegularExpression pattern; QTextCharFormat format; }; QVector<HighlightingRule> highlightingRules; QRegularExpression commentStartExpression; QRegularExpression commentEndExpression; QTextCharFormat keywordFormat; QTextCharFormat classFormat; QTextCharFormat singleLineCommentFormat; QTextCharFormat multiLineCommentFormat; QTextCharFormat quotationFormat; QTextCharFormat functionFormat; }; #endif // HIGHLIGHTER_H ``` 将`highlighter.cpp`里的代码修改为 ```css #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" << "\\bbool\\b"; foreach (const QString &pattern, keywordPatterns) { rule.pattern = QRegularExpression(pattern); rule.format = keywordFormat; highlightingRules.append(rule); } classFormat.setFontWeight(QFont::Bold); classFormat.setForeground(Qt::darkMagenta); rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b"); rule.format = classFormat; highlightingRules.append(rule); singleLineCommentFormat.setForeground(Qt::red); rule.pattern = QRegularExpression("//[^\n]*"); rule.format = singleLineCommentFormat; highlightingRules.append(rule); multiLineCommentFormat.setForeground(Qt::red); quotationFormat.setForeground(Qt::darkGreen); rule.pattern = QRegularExpression("\".*\""); rule.format = quotationFormat; highlightingRules.append(rule); functionFormat.setFontItalic(true); functionFormat.setForeground(Qt::blue); rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()"); rule.format = functionFormat; highlightingRules.append(rule); commentStartExpression = QRegularExpression("/\\*"); commentEndExpression = QRegularExpression("\\*/"); } void Highlighter::highlightBlock(const QString &text) { foreach (const HighlightingRule &rule, highlightingRules) { QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); while (matchIterator.hasNext()) { QRegularExpressionMatch match = matchIterator.next(); setFormat(match.capturedStart(), match.capturedLength(), rule.format); } } setCurrentBlockState(0); int startIndex = 0; if (previousBlockState() != 1) startIndex = text.indexOf(commentStartExpression); while (startIndex >= 0) { QRegularExpressionMatch match = commentEndExpression.match(text, startIndex); int endIndex = match.capturedStart(); int commentLength = 0; if (endIndex == -1) { setCurrentBlockState(1); commentLength = text.length() - startIndex; } else { commentLength = endIndex - startIndex + match.capturedLength(); } setFormat(startIndex, commentLength, multiLineCommentFormat); startIndex = text.indexOf(commentStartExpression, startIndex + commentLength); } } ``` 在`mainwindow.h`里添加 ```css //添加include文件 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include "highlighter.h" #include <QMainWindow> #include <QTextEdit> //文本 #include <QMenu> //加入菜单 #include <QMenuBar> //加入菜单栏 #include <QAction> //加入菜单栏 #include <QFileDialog> ``` 找到`private:`添加 ```css Highlighter *highlighter; ``` 如图 ![图4](https://s1.ax1x.com/2020/04/22/Jt3mcQ.png) 修改`mainwindow.cpp` ```css highlighter = new Highlighter(editor->document()); ``` 位置如图 ![图5](https://s1.ax1x.com/2020/04/22/Jt8vsH.png)