- 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());
}
曲折离奇,只能死记硬背了!
参考: