富文本
QTextEdit支持富文本处理,即文档中可使用多种格式,如文字、图片、表格等。与纯文本PlainText相对而言,windows的记事本就是纯文本编辑器,word就是富文本编辑器。
文档的光标主要基于QTextCursor类,文档的框架主要基于QTextDocument类。
一个富文本的文档结构主要分为几种元素:框架(QTextFrame)、文本块(QTextBlock)、表格(QTextTable)、和列表(QTextList)。
每种元素的格式有相应的format类表示:框架格式(QTextFrameFormat)、文本块格式(QTextBlockFormat)、表格格式(QTextTableFormat)、列表格式(QTextListFormat)。这些格式通常配合QTextCursor类使用。
QTextEdit类就是一个富文本编辑器,在构建QTextEdit类对象时就已经构建了一个QTextDocument类对象和一个QTextCursor类对象。只需调用他们进行相应的操作即可。
富文本编程-语法高亮
继续上一节的代码
右键点击项目名testRichText,添加C++类,类名MySyntaxHighlighter
,父类QSyntaxHighlighter
编辑MySyntaxHighlighter.h头文件
==================================================================
MySyntaxHighlighter.cpp编辑构造函数,重写highlighBlock事件
具体操作
具体代码
未雨绸缪还是给全代码方便以后查找
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mysyntaxhighlighter.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void showTextFrame(); //遍历文档框架
void showTextBlock(); //遍历文本块
void setTextFont(bool checked); //设置文本字体
void insertTable(); //插入表格
void insertList(); //插入列表
void insertImage(); //插入图片
private:
Ui::MainWindow *ui;
MySyntaxHighlighter *m_sLighter;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextFrame>
#include <QDebug>
#include <QFileDialog>
#include <QImage>
#include <QImageReader>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//获取文档对象
QTextDocument *document = ui->textEdit->document();
//获取根框架
QTextFrame *rootFrame = document->rootFrame();
//文档框架格式
QTextFrameFormat format;
format.setBorderBrush(Qt::red); //边框颜色
format.setBorder(3); //边框宽度
//文档框架设置格式
rootFrame->setFrameFormat(format);
//设置文本边框风格
QTextFrameFormat frameFormat;
frameFormat.setBackground(Qt::lightGray);
frameFormat.setMargin(10); //设置边距
frameFormat.setPadding(15); //设置填衬
frameFormat.setBorder(2);
frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dashed);
QTextCursor cursor = ui->textEdit->textCursor();
cursor.insertFrame(frameFormat);
QAction *action_textFrame = new QAction("框架", this);
connect(action_textFrame, &QAction::triggered, this, &MainWindow::showTextFrame);
ui->mainToolBar->addAction(action_textFrame);
QAction *action_textBlock = new QAction("文本块", this);
connect(action_textBlock, &QAction::triggered, this, &MainWindow::showTextBlock);
ui->mainToolBar->addAction(action_textBlock);
QAction *action_textFont = new QAction("字体", this);
action_textFont->setCheckable(true);
connect(action_textFont, &QAction::triggered, this, &MainWindow::setTextFont);
ui->mainToolBar->addAction(action_textFont);
QAction *action_textTable = new QAction("表格", this);
QAction *action_textList = new QAction("列表", this);
QAction *action_textIamge = new QAction("图片", this);
connect(action_textTable, &QAction::triggered, this, &MainWindow::insertTable);
connect(action_textList, &QAction::triggered, this, &MainWindow::insertList);
connect(action_textIamge, &QAction::triggered, this, &MainWindow::insertImage);
//工具栏添加动作按钮
ui->mainToolBar->addAction(action_textTable);
ui->mainToolBar->addAction(action_textList);
ui->mainToolBar->addAction(action_textIamge);
m_sLighter = new MySyntaxHighlighter(ui->textEdit->document());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showTextFrame()
{
//获取文档对象
QTextDocument *document = ui->textEdit->document();
//获取根框架
QTextFrame *frame = document->rootFrame();
QTextFrame::iterator it;
for (it = frame->begin(); !(it.atEnd()); ++it)
{
//获取当前框架指针
QTextFrame *childFrame = it.currentFrame();
//获取当前文本
QTextBlock childBlock = it.currentBlock();
if (childFrame)
qDebug() << "frame";
else if (childBlock.isValid())
qDebug() << "block" << childBlock.text();
}
}
void MainWindow::showTextBlock()
{
QTextDocument *document = ui->textEdit->document();
QTextBlock block = document->firstBlock();
//document->blockCount() 返回文本块个数
for (int i = 0; i < document->blockCount(); i++)
{
//输出文本信息
qDebug() << QString("文本块: %1, 文本块首行行号:%2, 长度%3, 内容%4")
.arg(i)
.arg(block.firstLineNumber())
.arg(block.length())
.arg(block.text());
block = block.next();
}
}
void MainWindow::setTextFont(bool checked)
{
if (checked)
{
QTextCursor cursor = ui->textEdit->textCursor();
//文本块格式
QTextBlockFormat blockFormat;
//居中对齐
blockFormat.setAlignment(Qt::AlignCenter);
cursor.insertBlock(blockFormat);
//字符格式
QTextCharFormat charFormat;
//字符背景色
charFormat.setBackground(Qt::lightGray);
//字符前景色(字符颜色)
charFormat.setForeground(Qt::blue);
//字体
charFormat.setFont(QFont(QString("宋体"), 12, QFont::Bold, true));
//下划线
charFormat.setFontUnderline(true);
//设置字符格式
cursor.setCharFormat(charFormat);
cursor.insertText("哈哈哈哈哈");
}
}
void MainWindow::insertTable()
{
QTextCursor cursor = ui->textEdit->textCursor();
QTextTableFormat format; //表格格式
format.setCellSpacing(2); //表格外变白
format.setCellPadding(10); //表格内边白
cursor.insertTable(3, 3, format);
}
void MainWindow::insertList()
{
QTextListFormat format; //列表格式
format.setStyle(QTextListFormat::ListDecimal); //数字编号
ui->textEdit->textCursor().insertList(format);
}
void MainWindow::insertImage()
{
QString filepath = QFileDialog::getOpenFileName(this, "选择图片", ".", "JPEG(.jpg *.jpeg);;GIG(*gif);;PNG(*.png)");
QUrl url(QString("file://%1").arg(filepath));
QImage image = QImageReader(filepath).read();
QTextDocument *document = ui->textEdit->document();
//文档添加图片资源
document->addResource(QTextDocument::ImageResource, url, QVariant(image));
QTextCursor cursor = ui->textEdit->textCursor();
QTextImageFormat imgFormat;
imgFormat.setWidth(image.width());
imgFormat.setHeight(image.height());
imgFormat.setName(url.toString());
cursor.insertImage(imgFormat);
}
mysyntaxthlighter.h
#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
mysyntaxhighlighter.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));
}
}
main.cpp
没动过
结语:
时间: 2020-08-12