<1> QTextEdit支持富文本处理,即文档中可使用多种格式,如文字、图片、表格等…
<2> 文档的光标主要基于QTextCursor类,文档的框架主要基于QTextDocument类。
<3> 一个富文本的文档结构主要分为几种元素:框架(QTextFrame)、文本块(QTextBlock)、表格(QTextTable)、和列表(QTextList)
<4> 每种元素的格式有相应的format类表示:框架(QTextFrameFormat)文本块格式(QTextBlockFormat)、表格格式(QTextTableFormat)、列表格式(QTextListFormat)。这些格式通常配合QTextCursor类使用。
<5> QTextEdit类就是一个富文本编辑器,在构建QTextEdit类对象时就已经构建了一个QTextDocument类对象和一个QTextCursor类对象。只需调用他们进行相应的操作即可。
1–文本边框格式
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);
2.文本框格式、文本块格式、字符格式
private slots:
void showTextFrame();//遍历文档框架
void showTextBlock();//遍历文本块
void setTextFont(bool checked);//设置文本字体
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("嘻嘻嘻嘻嘻嘻嘻");
}
}
QAction *action_textFrame = new QAction("框架",this);
connect(action_textFrame,&QAction::triggered,this,&MainWindow::showTextFrame);
ui->mainToolBar->addAction(action_textFrame);
QAction *action_block = new QAction("文本块",this);
connect(action_block,&QAction::triggered,this,&MainWindow::showTextBlock);
ui->mainToolBar->addAction(action_block);
QAction *action_font = new QAction("字体",this);
action_font->setCheckable(true);
connect(action_font,&QAction::triggered,this,&MainWindow::setTextFont);
ui->mainToolBar->addAction(action_font);
3.文档插入表格、列表、图片
private slots:
void insertTable();//插入表格
void insertList();//插入列表
void insertImage();//插入图片
//插入表格
void MainWindow::insertTable()
{
QTextCursor cursor = ui->textEdit->textCursor();
QTextTableFormat format;//表格格式
format.setCellSpacing(2);//表格外边白
format.setCellPadding(5);//表格内边白
cursor.insertTable(4,4,format);
}
//插入表格
void MainWindow::insertList()
{
QTextListFormat format;//列表格式
format.setStyle(QTextListFormat::ListDecimal);//数字编码
ui->textEdit->textCursor().insertList(format);
}
//插入图片
void MainWindow::insertImage()
{
QString filePath = QFileDialog::getOpenFileName(this,
"选择图片",
".",
"PNG(*.png);;"
"JPEG(*.jpg *.jpeg);;"
"GIF(.gif)");
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 format;
format.setWidth(image.width());
format.setHeight(image.height());
format.setName(url.toString());
cursor.insertImage(format);
}
QAction *action_table = new QAction("表格",this);
QAction *action_list = new QAction("列表",this);
QAction *action_image = new QAction("图片",this);
connect(action_table,&QAction::triggered,this,&MainWindow::insertTable);
connect(action_list,&QAction::triggered,this,&MainWindow::insertList);
connect(action_image,&QAction::triggered,this,&MainWindow::insertImage);
ui->mainToolBar->addAction(action_table);
ui->mainToolBar->addAction(action_list);
ui->mainToolBar->addAction(action_image);
4.语法高亮功能(设置关键字变色)
#include<QSyntaxHighlighter>
class mySyntaxHighLighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
mySyntaxHighLighter(QTextDocument *parent = 0);
protected:
//重写次方法
void highlightBlock(const QString &text);
};
#include "mysyntaxhighlighter.h"
mySyntaxHighLighter::mySyntaxHighLighter(QTextDocument *parent):QSyntaxHighlighter(parent)
{
}
void mySyntaxHighLighter::highlightBlock(const QString &text)
{
QTextCharFormat format;//字符格式
format.setFontWeight(QFont::Bold);//加粗
format.setBackground(Qt::blue);
format.setForeground(Qt::red);
QString patter = "\\bgood\\b";//匹配单词边界
QRegExp expression(patter);
int index = text.indexOf(expression);
while(index >= 0){
int length = expression.matchedLength();
setFormat(index,length,format);
index = text.indexOf(expression,index + length);
}
}
private://.h中
mySyntaxHighLighter *m_sLinghter;
//语法高亮 .cpp中
m_sLinghter = new mySyntaxHighLighter(ui->textEdit->document());
5.查找功能
private slots:
void textFind();//找查文本
void textNext();//找查下一个
private:
QLineEdit *m_lineEdit;
QDialog *m_findDialog;
void MainWindow::textFind()
{
m_findDialog->show();
}
void MainWindow::textNext()
{
QString str = m_lineEdit->text();
bool isFind = ui->textEdit->find(str,QTextDocument::FindBackward);
if(isFind){
qDebug() << QString("行号:%1,列好:%2")
.arg(ui->textEdit->textCursor().blockNumber())
.arg(ui->textEdit->textCursor().columnNumber());
}
}
//查找
QAction *action_textfind = new QAction("找查",this);
connect(action_textfind,&QAction::triggered,this,&MainWindow::textFind);
ui->mainToolBar->addAction(action_textfind);
m_findDialog = new QDialog(this);//查找对话框
m_lineEdit = new QLineEdit(m_findDialog);//查找输入框
QPushButton *btn = new QPushButton(m_findDialog);
btn->setText("查找下一个");
connect(btn,&QPushButton::clicked,this,&MainWindow::textNext);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(m_lineEdit);
layout->addWidget(btn);
m_findDialog->setLayout(layout);
6.代码总和
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"mysyntaxhighlighter.h"
#include<QLineEdit>
#include<QDialog>
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();//插入图片
void textFind();//找查文本
void textNext();//找查下一个
private:
Ui::MainWindow *ui;
mySyntaxHighLighter *m_sLinghter;
QLineEdit *m_lineEdit;
QDialog *m_findDialog;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTextFrame>
#include<QDebug>
#include<QFileDialog>
#include<QImageReader>
#include<QPushButton>
#include<QLineEdit>
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_block = new QAction("文本块",this);
connect(action_block,&QAction::triggered,this,&MainWindow::showTextBlock);
ui->mainToolBar->addAction(action_block);
QAction *action_font = new QAction("字体",this);
action_font->setCheckable(true);
connect(action_font,&QAction::triggered,this,&MainWindow::setTextFont);
ui->mainToolBar->addAction(action_font);
QAction *action_table = new QAction("表格",this);
QAction *action_list = new QAction("列表",this);
QAction *action_image = new QAction("图片",this);
connect(action_table,&QAction::triggered,this,&MainWindow::insertTable);
connect(action_list,&QAction::triggered,this,&MainWindow::insertList);
connect(action_image,&QAction::triggered,this,&MainWindow::insertImage);
ui->mainToolBar->addAction(action_table);
ui->mainToolBar->addAction(action_list);
ui->mainToolBar->addAction(action_image);
//语法高亮
m_sLinghter = new mySyntaxHighLighter(ui->textEdit->document());
//查找
QAction *action_textfind = new QAction("找查",this);
connect(action_textfind,&QAction::triggered,this,&MainWindow::textFind);
ui->mainToolBar->addAction(action_textfind);
m_findDialog = new QDialog(this);//查找对话框
m_lineEdit = new QLineEdit(m_findDialog);//查找输入框
QPushButton *btn = new QPushButton(m_findDialog);
btn->setText("查找下一个");
connect(btn,&QPushButton::clicked,this,&MainWindow::textNext);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(m_lineEdit);
layout->addWidget(btn);
m_findDialog->setLayout(layout);
}
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(5);//表格内边白
cursor.insertTable(4,4,format);
}
//插入表格
void MainWindow::insertList()
{
QTextListFormat format;//列表格式
format.setStyle(QTextListFormat::ListDecimal);//数字编码
ui->textEdit->textCursor().insertList(format);
}
//插入图片
void MainWindow::insertImage()
{
QString filePath = QFileDialog::getOpenFileName(this,
"选择图片",
".",
"PNG(*.png);;"
"JPEG(*.jpg *.jpeg);;"
"GIF(.gif)");
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 format;
format.setWidth(image.width());
format.setHeight(image.height());
format.setName(url.toString());
cursor.insertImage(format);
}
void MainWindow::textFind()
{
m_findDialog->show();
}
void MainWindow::textNext()
{
QString str = m_lineEdit->text();
bool isFind = ui->textEdit->find(str,QTextDocument::FindBackward);
if(isFind){
qDebug() << QString("行号:%1,列好:%2")
.arg(ui->textEdit->textCursor().blockNumber())
.arg(ui->textEdit->textCursor().columnNumber());
}
}
mySyntaxHighLighter.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::blue);
format.setForeground(Qt::red);
QString patter = "\\bgood\\b";//匹配单词边界
QRegExp expression(patter);
int index = text.indexOf(expression);
while(index >= 0){
int length = expression.matchedLength();
setFormat(index,length,format);
index = text.indexOf(expression,index + length);
}
}