“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

I. Function introduction

When editing or displaying text using QTextEdit, it is often necessary to implement a keyword, or some specified text coloring, highlighting, or highlighting. For example: we often write code IDE software, the interface can be based on different languages, different keywords to complete a variety of color highlighting, this function QT QTextEdit can be fully implemented, and QT official also gives examples of code. This article refers to the official provided example code ideas, inherit the QSyntaxHighlighter class, rewrite the highlightBlock function, set their own keywords.

Set the colored text to support the regular expression statement, the core code example is as follows:

void Highlighter::highlightBlock(const QString &text)
{
    if(word_text.isEmpty())return;

    QTextCharFormat myClassFormat;
    myClassFormat.setFontWeight(QFont::Bold);
    myClassFormat.setForeground(Qt::darkMagenta);

    // Support regular expressions
    QString pattern = word_text;

    QRegExp expression(pattern);
    int index = text.indexOf(expression);
    while (index >= 0) {
        int length = expression.matchedLength(a);setFormat(index, length, myClassFormat);
        index = text.indexOf(expression, index + length); }}Copy the code

Two, implementation method

2.1 Highlighter. CPP

This is the CPP source file that inherits the QSyntaxHighlighter class.


#include "highlighter.h"

/ /! [0]
Highlighter::Highlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{

}


void Highlighter::highlightBlock(const QString &text)
{
    if(word_text.isEmpty())return;

    QTextCharFormat myClassFormat;
    myClassFormat.setFontWeight(QFont::Bold);
    myClassFormat.setForeground(Qt::darkMagenta);
    QString pattern = word_text;

    QRegExp expression(pattern);
    int index = text.indexOf(expression);
    while (index >= 0) {
        int length = expression.matchedLength(a);setFormat(index, length, myClassFormat);
        index = text.indexOf(expression, index + length); }}/* Project: QTextEdit_Highlight_test Date: 2021-10-30 Author: DS Xiaolenggui environment: Win10 QT5.12.6 MinGW32 feature: highlighted text Settings */
void Highlighter::SetText(QString text)
{
    word_text=text;
}

Copy the code

2.2 Highlighter. H


#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H

#include <QSyntaxHighlighter>
#include <QTextCharFormat>

QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE

/ /! [0]
class Highlighter : public QSyntaxHighlighter
{
    Q_OBJECT

public:
    Highlighter(QTextDocument *parent = 0);
    void SetText(QString text);
protected:
    void highlightBlock(const QString &text) Q_DECL_OVERRIDE;

    QString word_text;
};
/ /! [0]

#endif // HIGHLIGHTER_H
Copy the code

2.3 the widget. The CPP

#include "widget.h"
#include "ui_widget.h"

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

    highlighter = new Highlighter(ui->textEdit->document());

}

Widget::~Widget()
{
    delete ui;
}

/* Project: QTextEdit_Highlight_test Date: 2021-10-30 Author: DS Small Ryugo environment: Win10 QT5.12.6 MinGW32 function: set the highlighted text */
void Widget::on_pushButton_clicked(a)
{
    highlighter->SetText(ui->lineEdit->text());
}
Copy the code

2.4 widget. H

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "highlighter.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget(a); Highlighter *highlighter;private slots:
    void on_pushButton_clicked(a);

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
Copy the code

2.5 UI Design Interface