富文本处理
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextFrame>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//富文本或者富文本格式,简单来说就是在文档中可以使用多种格式,比如字体颜色,图片和表格等。
//一个富文本文档的结构分为几种元素类表示,分别是框架(QTextFrame),文本块(QTextBlock),
//表格(QTextTable)和列表(QTextList)。
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(5); //设置填衬
frameFormat.setBorder(2);
frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); //设置边框样式
QTextCursor cursor = ui->textEdit->textCursor(); //获取光标
cursor.insertFrame(frameFormat); //在光标处插入框架
//文本块QTextBlock类为文本文档QTextDocument提供了一个文本片段(QTextFragment)的容器。一个文本块
//可以看做一个段落,但是不能使用回车换行,因为回车换行就表示创建一个新的文本块。文本块的格式由
//QTextBlockFormat类来处理,主要涉及对齐方式,文本块四周的边白、缩进等内容。而文本块中的文本内容的格式
//比如字体大小,加粗,下划线等内容,则由QTextCharFormat类来设置。
QAction *action_textFrame = new QAction(tr("框架"),this);
connect(action_textFrame,SIGNAL(triggered()),this,SLOT(showTextFrame()));
ui->mainToolBar->addAction(action_textFrame); //在工具栏添加动作
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showTextFrame() //遍历框架
{
QTextDocument *document = ui->textEdit->document();
QTextFrame *frame = document->rootFrame();
QTextFrame::iterator it; //建立QTextFrame类的迭代器
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();
}
}
可以看到这里只能输出根框架中的文本块和子框架,而子框架中的文本块却无法遍历到。遍历所有文本块可以使用下列方法:
继续添加代码,红色标示。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextFrame>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//富文本或者富文本格式,简单来说就是在文档中可以使用多种格式,比如字体颜色,图片和表格等。
//一个富文本文档的结构分为几种元素类表示,分别是框架(QTextFrame),文本块(QTextBlock),
//表格(QTextTable)和列表(QTextList)。
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(5); //设置填衬
frameFormat.setBorder(2);
frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); //设置边框样式
QTextCursor cursor = ui->textEdit->textCursor(); //获取光标
cursor.insertFrame(frameFormat); //在光标处插入框架
//文本块QTextBlock类为文本文档QTextDocument提供了一个文本片段(QTextFragment)的容器。一个文本块
//可以看做一个段落,但是不能使用回车换行,因为回车换行就表示创建一个新的文本块。文本块的格式由
//QTextBlockFormat类来处理,主要涉及对齐方式,文本块四周的边白、缩进等内容。而文本块中的文本内容的格式
//比如字体大小,加粗,下划线等内容,则由QTextCharFormat类来设置。
QAction *action_textFrame = new QAction(tr("框架"),this);
connect(action_textFrame,SIGNAL(triggered()),this,SLOT(showTextFrame()));
ui->mainToolBar->addAction(action_textFrame); //在工具栏添加动作
QAction *action_textBlock = new QAction(tr("文本块"), this);
connect(action_textBlock,SIGNAL(triggered()),this,SLOT(showTextBlock()));
ui->mainToolBar->addAction(action_textBlock);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showTextFrame() //遍历框架
{
QTextDocument *document = ui->textEdit->document();
QTextFrame *frame = document->rootFrame();
QTextFrame::iterator it; //建立QTextFrame类的迭代器
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(); //获取文档的第一个文本块
for (int i = 0; i < document->blockCount(); ++i){
qDebug() << tr("文本块 %1,文本块首行行号为 %2,长度 %3,内容为:")
.arg(i).arg(block.firstLineNumber()).arg(block.length())
<< block.text();
block = block.next(); //获取下一个文本块
}
}程序运行后,按下““文本块”动作,结果为:
还可以进一步编辑文本块及其内容的格式
在mainwindow.h 中添加私有槽声明
void setTextFont(bool checked);
在mainwindow.cpp 中继续添加代码,蓝色字体标示:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextFrame>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//富文本或者富文本格式,简单来说就是在文档中可以使用多种格式,比如字体颜色,图片和表格等。
//一个富文本文档的结构分为几种元素类表示,分别是框架(QTextFrame),文本块(QTextBlock),
//表格(QTextTable)和列表(QTextList)。
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(5); //设置填衬
frameFormat.setBorder(2);
frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); //设置边框样式
QTextCursor cursor = ui->textEdit->textCursor(); //获取光标
cursor.insertFrame(frameFormat); //在光标处插入框架
//文本块QTextBlock类为文本文档QTextDocument提供了一个文本片段(QTextFragment)的容器。一个文本块
//可以看做一个段落,但是不能使用回车换行,因为回车换行就表示创建一个新的文本块。文本块的格式由
//QTextBlockFormat类来处理,主要涉及对齐方式,文本块四周的边白、缩进等内容。而文本块中的文本内容的格式
//比如字体大小,加粗,下划线等内容,则由QTextCharFormat类来设置。
QAction *action_textFrame = new QAction(tr("框架"),this);
connect(action_textFrame,SIGNAL(triggered()),this,SLOT(showTextFrame()));
ui->mainToolBar->addAction(action_textFrame); //在工具栏添加动作
QAction *action_textBlock = new QAction(tr("文本块"), this);
connect(action_textBlock,SIGNAL(triggered()),this,SLOT(showTextBlock()));
ui->mainToolBar->addAction(action_textBlock);
QAction *action_Font = new QAction(tr("字体"), this);
action_Font->setCheckable(true); //设置动作可以被选中
connect(action_Font,SIGNAL(toggled(bool)),this,SLOT(setTextFont(bool)));
ui->mainToolBar->addAction(action_Font);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showTextFrame() //遍历框架
{
QTextDocument *document = ui->textEdit->document();
QTextFrame *frame = document->rootFrame();
QTextFrame::iterator it; //建立QTextFrame类的迭代器
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(); //获取文档的第一个文本块
for (int i = 0; i < document->blockCount(); ++i){
qDebug() << tr("文本块 %1,文本块首行行号为 %2,长度 %3,内容为:")
.arg(i).arg(block.firstLineNumber()).arg(block.length())
<< 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::green); //背景色
charFormat.setForeground(Qt::blue); //字体颜色
//使用宋体,12号,加粗,倾斜
charFormat.setFont(QFont(tr("宋体"),12,QFont::Bold,true));
charFormat.setFontUnderline(true); //使用下划线
cursor.setCharFormat(charFormat); //使用字符格式
cursor.insertText(tr("测试字体")); //插入文本
}else {
//处于非选中状态可以进行其他操作
}
}
运行结果如下: