QT QTextEdit富文本插入字体-表格-编号-图片与查找功能,输入char 自动变成蓝色-语法高亮功能
QTQTextEdit富文本插入字体-表格-编号-图片-查找-语法高亮功能.rar-QT文档类资源-CSDN下载QTQTextEdit富文本插入字体-表格-编号-图片-查找-语法高亮功能.rarhttps:/更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/txwtech/86507476
什么是富文本?
word 上的字体可以更改,变换颜色等的是rich text 富文本
而window记事本那种是纯文本(plain text)
关于它的帮助可以看助手 Rich Text Processing 关键字查找
富文本 文档结构
QT对富文本的处理只有只读和编辑二种方式
对于文档的读取和编辑要使用二种不同的接口
文档的光标主要基于QTextCursor类,而文档的框架主要基于QTextDocument类。
一个富文本文档的结构分为几种元素来表示,分别是框架(QTextFrame)、文本块(QTextBlock)、表格(QTextTable)和列表(QTextList)。
每种元素的格式又使用相应的 format类来表示,分别是框架格式(QTextFrameFormat)、文本块格式(QTextBlockFormat),表格格式(QTextTableFormat)和列表格式(QTextListFormat),这些格式一般在编辑文档时使用,所以常和QTextCursor类配合使用。
QTextEdit类就是一个富文本编辑器,所以在构造QTexrEdit类的对象时就已经构建了一个QTextDocument类对象和一个QTextCursor类对象,只须调用它们进行相应的操作即可
一个空的文档包含了一个根框架(Root frame)
,这个根框架又包含了一个空的文本块(Block)。
框架将一个文档分为多个部分,在根框架里可以再添加文本块、子框架和表格等,一个文档的结构
#ifndef MAINWINDOW5_3_H
#define MAINWINDOW5_3_H#include <QMainWindow>
class QLineEdit;
class QDialog;
class MySyntaxHiglighter;QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow5_3; }
QT_END_NAMESPACEclass MainWindow5_3 : public QMainWindow
{Q_OBJECTpublic:MainWindow5_3(QWidget *parent = nullptr);~MainWindow5_3();private:Ui::MainWindow5_3 *ui;QLineEdit *lineEdit; //对象指针QDialog *findDialog;MySyntaxHiglighter *my_highlighter;private slots:void showTextFrame();//获取文本框架void showTextBlock();//获取文本块void setTextFont(bool checked);void insertTable();void insertList();void insertImage();void textFind();void findNext();void findPrevious();
};
#endif // MAINWINDOW5_3_H
#include "mainwindow5_3.h"
#include "ui_mainwindow5_3.h"
#include <QTextFrame>
#include <QDebug>
#include <QLineEdit>
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
#include "mysyntaxhiglighter.h"
//编辑器中插入表格,列表,图片的方法
MainWindow5_3::MainWindow5_3(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow5_3)
{ui->setupUi(this);QTextDocument *qDoc=ui->textEdit->document();//获取文档对象QTextFrame *root_frame = qDoc->rootFrame();//获取根框架QTextFrameFormat q_format;//创建框架的格式q_format.setBorderBrush(Qt::red);//边界的颜色q_format.setBorder(2);//边界线的宽度,粗细// root_frame->setFrameFormat(q_format);//框架使用格式QTextFrameFormat frameFormat;frameFormat.setBackground(Qt::darkGreen);frameFormat.setMargin(1);//边距frameFormat.setPadding(2);//填衬frameFormat.setBorder(2);frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted);//边框样式//QTextCursor cursor2=ui->textEdit->textCursor();//获取光标//cursor2.insertFrame(frameFormat);//在光标处插入框架//获取框架 QTextFrame操作QAction *action_textFrame=new QAction(tr("框架"),this);connect(action_textFrame,&QAction::triggered,this,&MainWindow5_3::showTextFrame);ui->toolBar->addAction(action_textFrame);//获取文本块 QTextBlock操作QAction *action_block= new QAction(tr("文本块"),this);connect(action_block,&QAction::triggered,this,&MainWindow5_3::showTextBlock);ui->toolBar->addAction(action_block);//设置字体QAction * action_font=new QAction(tr("字体"),this);action_font->setCheckable(true);//connect(action_font,&QAction::triggered,this,&MainWindow5_3::setTextFont);connect(action_font,&QAction::toggled,this,&MainWindow5_3::setTextFont);ui->toolBar->addAction(action_font);////编辑器中插入表格,列表,图片的方法QAction *action_text_table=new QAction(tr("插入表格"),this);QAction *action_text_list=new QAction(tr("插入列表编号"),this);QAction *action_text_image=new QAction(tr("插入图片"),this);connect(action_text_table,&QAction::triggered,this,&MainWindow5_3::insertTable);connect(action_text_list,&QAction::triggered,this,&MainWindow5_3::insertList);connect(action_text_image,&QAction::triggered,this,&MainWindow5_3::insertImage);ui->toolBar->addAction(action_text_table);ui->toolBar->addAction(action_text_list);ui->toolBar->addAction(action_text_image);//实现查找功能QAction *action_text_find=new QAction(tr("查找"),this);connect(action_text_find,&QAction::triggered,this,&MainWindow5_3::textFind);//绑定工具栏的点击信号到执行函数ui->toolBar->addAction(action_text_find);findDialog=new QDialog(this);findDialog->setWindowTitle(tr("查找功能"));lineEdit = new QLineEdit(findDialog);//创建行编辑器QPushButton * q_pushbutton=new QPushButton(findDialog);//创建按钮// QPushButton * q_pushbutton_upstream=new QPushButton(findDialog);//创建按钮q_pushbutton->setText(tr("查找上一个"));// q_pushbutton_upstream->setText(tr("查找上一个"));connect(q_pushbutton,&QPushButton::clicked,this,&MainWindow5_3::findNext);// connect(q_pushbutton_upstream,&QPushButton::clicked,this,&MainWindow5_3::findPrevious);QVBoxLayout *qvbox_layout=new QVBoxLayout;//创建垂直布局管理器qvbox_layout->addWidget(lineEdit);qvbox_layout->addWidget(q_pushbutton);//qvbox_layout->addWidget(q_pushbutton_upstream);findDialog->setLayout(qvbox_layout);//语法高亮测试my_highlighter= new MySyntaxHiglighter(ui->textEdit->document());//创建MySyntaxHiglighter类对象//使用ui->textEdit->document()作为参数,文档改变将触发MySyntaxHiglighter的highlightBlock函数来设置语法高亮//QTextEdit 富文本 还支持HTML 子集ui->textEdit->append(tr("<h1><font color=gray> 测试使用HTML方式</font></h1>"));}MainWindow5_3::~MainWindow5_3()
{delete ui;
}
//获取框架 QTextFrame操作
void MainWindow5_3::showTextFrame()
{QTextDocument *q_doc =ui->textEdit->document();QTextFrame *q_frame = q_doc->rootFrame();QTextFrame::iterator frame_iterator;for (frame_iterator=q_frame->begin();!(frame_iterator.atEnd());++frame_iterator){QTextFrame *child_frame=frame_iterator.currentFrame();//获取当前框架的指针QTextBlock childBlock=frame_iterator.currentBlock();//获取当前文本块if(child_frame){qDebug()<<"frame";}else if(childBlock.isValid()){qDebug()<<"block:"<<childBlock.text();}}
}
//获取文本块 QTextBlock操作
void MainWindow5_3::showTextBlock()
{QTextDocument *qdoc= ui->textEdit->document();QTextBlock qblock=qdoc->firstBlock();//获取文档的第一个文本块哦for(int i=0;i<qdoc->blockCount();i++){qDebug()<<tr("文本块:%1,首行行号:%2,长度:%3,内容:").arg(i).arg(qblock.firstLineNumber()).arg(qblock.length())<<qblock.text();qblock=qblock.next();}}void MainWindow5_3::setTextFont(bool checked)
{if(checked)//处于选中状态{QTextCursor qcursor=ui->textEdit->textCursor();QTextBlockFormat block_format;//文本块格式block_format.setAlignment(Qt::AlignCenter);//水平居中qcursor.insertBlock(block_format);//插入文本块的格式QTextCharFormat char_format; //字符格式char_format.setBackground(Qt::blue);//背景色char_format.setForeground(Qt::yellow);//字体颜色char_format.setFont(QFont(tr("宋体"),12,QFont::Bold,true));//宋体12号,加粗,斜体字char_format.setFontUnderline(true);//使用下划线qcursor.setCharFormat(char_format);qcursor.insertText(tr("测试一下字体呢"));}else{qDebug()<<tr("字体未设置");}
}void MainWindow5_3::insertTable()
{QTextCursor qcursor=ui->textEdit->textCursor();QTextTableFormat qtext_table_format;qtext_table_format.setCellSpacing(2);qtext_table_format.setCellPadding(10);qcursor.insertTable(2,2,qtext_table_format);//插入2行2列
}void MainWindow5_3::insertList()
{QTextListFormat qtext_list_format;//列表格式qtext_list_format.setStyle(QTextListFormat::ListDecimal);//数字编码ui->textEdit->textCursor().insertList(qtext_list_format);}void MainWindow5_3::insertImage()//插入图片
{QTextImageFormat qtext_image_format;//qtext_image_format.setName("../xxx/duck_dance.png");//qtext_image_format.setName("./duck_dance.png"); //图片放在debug的文件夹里面qtext_image_format.setName("duck_dance.png");//图片放在debug的文件夹里面ui->textEdit->textCursor().insertImage(qtext_image_format);
}void MainWindow5_3::textFind()
{findDialog->show();
}void MainWindow5_3::findNext()
{QString str2=lineEdit->text();//使用查找函数查找指定字符串,查找方式为向后查找// bool b_find=ui->textEdit->find(str2,QTextDocument::FindBackward);bool b_find=ui->textEdit->find(str2,QTextDocument::FindBackward);/** 默认向前查找* FindBackward向后* FindCaseSensitively = 0x00002,不区分大小写FindWholeWords = 0x00004,匹配整个单词**/if(b_find){qDebug()<<tr("行号:%1,列号:%2").arg(ui->textEdit->textCursor().blockNumber()).arg(ui->textEdit->textCursor().columnNumber());}else{qDebug()<<tr("未查找到信息");ui->textEdit->moveCursor(QTextCursor::End);//光标移动到句末尾//ui->textEdit->moveCursor(QTextCursor::Start);//光标移动到开始}
}void MainWindow5_3::findPrevious()
{QString str2=lineEdit->text();//使用查找函数查找指定字符串,查找方式为向后查找// bool b_find=ui->textEdit->find(str2,QTextDocument::FindBackward);bool b_find=ui->textEdit->find(str2,QTextDocument::FindBackward);/** 默认向前查找* FindBackward向后* FindCaseSensitively = 0x00002,不区分大小写FindWholeWords = 0x00004,匹配整个单词**/if(b_find){qDebug()<<tr("行号:%1,列号:%2").arg(ui->textEdit->textCursor().blockNumber()).arg(ui->textEdit->textCursor().columnNumber());}else{qDebug()<<tr("未查找到信息");}
}
语法高亮类文件
#ifndef MYSYNTAXHIGLIGHTER_H
#define MYSYNTAXHIGLIGHTER_H
#include <QSyntaxHighlighter>class MySyntaxHiglighter : public QSyntaxHighlighter
{Q_OBJECT
public:explicit MySyntaxHiglighter(QTextDocument *parent =0);
protected:void highlightBlock(const QString &text) override;//重新实现该函数
};#endif // MYSYNTAXHIGLIGHTER_H
#include "mysyntaxhiglighter.h"MySyntaxHiglighter::MySyntaxHiglighter(QTextDocument *parent):QSyntaxHighlighter(parent)
{}void MySyntaxHiglighter::highlightBlock(const QString &text)
{QTextCharFormat my_format;my_format.setFontWeight(QFont::Bold);my_format.setForeground(Qt::blue);QString pattern2="\\bchar\\b";//匹配charQRegExp expression2(pattern2);int index2=text.indexOf(expression2);//从位置0开始匹配字符串while(index2>=0){int length2=expression2.matchedLength();setFormat(index2,length2,my_format);//对要匹配的字符串设置格式index2=text.indexOf(expression2,index2+length2);//继续匹配}
}