0
点赞
收藏
分享

微信扫一扫

yarn dev报错X [ERROR] Cannot assign to “i“ because it is a constant

点亮自己的那盏灯 2024-06-05 阅读 5

1.添加新文件mysyntaxhighlighter继承QSyntaxHighlighter

#ifndef MYSYNTAXHIGHLIGHTER_H
#define MYSYNTAXHIGHLIGHTER_H

#include<QSyntaxHighlighter>

class MySyntaxhighlighter : public QSyntaxHighlighter
{
    Q_OBJECT //支持源对象系统属性
public:
    MySyntaxhighlighter(QTextDocument* parent = 0);
protected:
    //重写实现该方法
    void highlightBlock(const QString& text);
};

#endif // MYSYNTAXHIGHLIGHTER_H

2.在主头文件进行语法定义 MySyntaxhighlighter *m_sLighter; //语法定义及要包含新定义的头文件

3.在mainwindow.cpp里面添加到工具栏上

 m_sLighter = new MySyntaxhighlighter(ui->textEdit->document());

4. 在高亮cpp里面进行实现

#include "mysyntaxhighlighter.h"

MySyntaxhighlighter::MySyntaxhighlighter(QTextDocument* parent)
    :QSyntaxHighlighter(parent)
{

}

void MySyntaxhighlighter::highlightBlock(const QString &text)
{
    QTextCharFormat format; //字符格式
    format.setFontWeight(QFont::Bold); //加粗
    format.setBackground(Qt::red); //背景颜色
    format.setForeground(Qt::green); //字体颜色

    //语法高亮+进行匹配
    QString pattern = "\\bgood\\b"; //匹配单词边界
    QRegExp expression(pattern);
    int index = text.indexOf(expression);
    while(index >=0){
        int length = expression.matchedLength();//匹配到的字符长度
        setFormat(index,length,format);
        index = text.indexOf(expression,index + length);
    }
}
举报

相关推荐

0 条评论