qt QSyntaxHighlighter详解

1、概述

QSyntaxHighlighter是Qt文本处理框架中的一个强大工具,它专门用于实现文本编辑器中的语法高亮功能。通过自定义高亮规则,QSyntaxHighlighter可以实现对代码编辑器、富文本编辑器中的关键字、注释等内容的高亮显示。这一功能对于提升代码的可读性和编辑效率具有重要意义。

2、重要方法
  • QSyntaxHighlighter(QObject *parent):通过父对象构造。

  • QSyntaxHighlighter(QTextDocument *parent):通过文本文档构造。

  • void setDocument(QTextDocument *doc):设置语法高亮器关联的文本文档。

  • QTextDocument *document()const:返回语法高亮器关联的文本文档

  • virtual void highlightBlock(const Qstring &text):纯虚函数,需要在子类中实现,用于高亮显示指定文本块。

    //.h
    class SyntaxHighlighter : public QSyntaxHighlighter
    {
    Q_OBJECT

    public:
    SyntaxHighlighter(QTextDocument *parent = nullptr);

    protected:
    void highlightBlock(const QString &text) override;

    private:
    void setupHighlightingRules();

    复制代码
      struct HighlightingRule
      {
          QRegExp pattern;
          QTextCharFormat format;
      };
      QVector<HighlightingRule> highlightingRules;
    
      QTextCharFormat keywordFormat;
      QTextCharFormat commentFormat;

    };

    //.cpp
    SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
    {
    setupHighlightingRules();
    }

    void SyntaxHighlighter::setupHighlightingRules()
    {
    HighlightingRule rule;

    复制代码
      // 关键字高亮规则
      keywordFormat.setForeground(Qt::blue);
      keywordFormat.setFontWeight(QFont::Bold);
      QStringList keywordPatterns;
      keywordPatterns << "\\bint\\b" << "\\bfloat\\b" << "\\bdouble\\b" << "\\bchar\\b"
                      << "\\bvoid\\b" << "\\bif\\b" << "\\belse\\b" << "\\bfor\\b"
                      << "\\bwhile\\b" << "\\breturn\\b";
      foreach (const QString &pattern, keywordPatterns) {
          rule.pattern = QRegExp(pattern);
          rule.format = keywordFormat;
          highlightingRules.append(rule);
      }
    
      // 注释高亮规则
      commentFormat.setForeground(Qt::darkGray);
      commentFormat.setFontItalic(true);
      rule.pattern = QRegExp("//[^\n]*");
      rule.format = commentFormat;
      highlightingRules.append(rule);

    }

    void SyntaxHighlighter::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);
    }
    }
    }

    //main.cpp
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    复制代码
      QTextEdit textEdit;
      textEdit.setPlainText("int main() { if (true) { return 0; } }"); // 设置示例文本
    
      // 创建并应用Highlighter实例
      SyntaxHighlighter *highlighter = new SyntaxHighlighter(textEdit.document());
    
      textEdit.show();
    
      return a.exec();

    }

觉得有帮助的话,打赏一下呗。。

相关推荐
用户805533698031 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner1 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz6 天前
QML Hello World 入门示例
qt
xcyxiner9 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner10 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner10 天前
DicomViewer (添加模型类)3
qt
xcyxiner11 天前
DicomViewer (目录调整) 2
qt
xcyxiner11 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能13 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G13 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt