富文本(关键字高亮)

  • Qt的富文本处理中提供了QSyntaxHighlighter类实现语法高亮
  • 重新实现highlightBlock()函数,直接将QTextDocument类对象指针作为其父部件指针,就可自动调用highlightBlock()函数。

???

代码:

#ifndef MYSYNTAXHIGHLIGHTER_H
#define MYSYNTAXHIGHLIGHTER_H

#include <QObject>
#include <QSyntaxHighlighter>
#include <QTextDocument>
class MySyntaxHighlighter : public QSyntaxHighlighter
{
    Q_OBJECT
public:
    explicit MySyntaxHighlighter(QTextDocument *parent = nullptr);

signals:

public slots:
protected:
    // 高亮文本框,必须实现该函数
    void highlightBlock(const QString &text);
};

#endif // MYSYNTAXHIGHLIGHTER_H

 

#include "mysyntaxhighlighter.h"

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

}

void MySyntaxHighlighter::highlightBlock(const QString &text)
{

    // 创建匹配到的字符串将要显示的格式
    QTextCharFormat highLightFormat;
    highLightFormat.setFontWeight(QFont::Bold);
    highLightFormat.setForeground(Qt::blue);

    // 创建 pattern,开始匹配,匹配了就改变 charFormat
    QString pattern = "\\bI like apple\\b";
    QRegExp expression(pattern);
    int index = text.indexOf(expression);

    while(index >= 0)
    {
        int length = expression.matchedLength();
        setFormat(index,length,highLightFormat);

        index = text.indexOf(expression,index + length);
    }
}

 


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // get Document Obj
    QTextDocument *document = ui->textEdit->document();

    // get root frame
    QTextFrame *rootFrame = document->rootFrame();

    // create Formate of rootFrame
    QTextFrameFormat format;
    format.setBorderBrush(Qt::blue);
    format.setBorder(2);
    rootFrame->setFrameFormat(format);

    // create a subFrame in rootFrame
    QTextFrameFormat sub_format;
    sub_format.setBackground(Qt::gray);
    sub_format.setMargin(8); // outside distance
    sub_format.setPadding(10); // inside distance
    sub_format.setBorder(3); // border width
    sub_format.setBorderStyle(QTextFrameFormat::BorderStyle_Dashed);

    // insert formate in cursor's position
    QTextCursor cursor = ui->textEdit->textCursor();
    cursor.insertFrame(sub_format);

    // 测试TextCharFormat
    QTextCharFormat charFormat;
    charFormat.setBackground(Qt::blue);
    charFormat.setForeground(Qt::red);
    charFormat.setFont(QFont(tr("微软雅黑"),18,QFont::Bold));
    cursor.setCharFormat(charFormat);
    // cursor.insertText("我喜欢吃水果!");

    // ----
    QAction *action = new QAction(tr("showTextFrame"),this);
    connect(action,&QAction::triggered,this,&MainWindow::showTextFrame);
    ui->mainToolBar->addAction(action);

    QAction *action_2 = new QAction(tr("showTextBlock"),this);
    connect(action_2,&QAction::triggered,this,&MainWindow::showTextBlock);
    ui->mainToolBar->addAction(action_2);

    QAction *action_3 = new QAction(tr("List"),this);
    connect(action_3,&QAction::triggered,this,&MainWindow::insertList);
    ui->mainToolBar->addAction(action_3);

    QAction *action_4 = new QAction(tr("tab"),this);
    connect(action_4,&QAction::triggered,this,&MainWindow::insertTable);
    ui->mainToolBar->addAction(action_4);

    QAction *action_5 = new QAction(tr("Image"),this);
    connect(action_5,&QAction::triggered,this,&MainWindow::insertImage);
    ui->mainToolBar->addAction(action_5);

    QAction *action_6 = new QAction(tr("Find"),this);
    connect(action_6,&QAction::triggered,this,&MainWindow::findNext);
    ui->mainToolBar->addAction(action_6);

    // 高亮
    syntaxHighlighter = new MySyntaxHighlighter(ui->textEdit->document());
}

曲折离奇,只能死记硬背了!

 

参考:

不二如是:https://fishc.com.cn/forum.php?mod=viewthread&tid=77129&extra=page%3D4%26filter%3Dauthor%26orderby%3Ddateline%26typeid%3D449

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sssnial-jz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值