Qt富文本编辑器QTextDocument
本文标题:Qt富文本编辑器QTextDocument 本文地址:https://www.techieliang.com/2017/12/726/
1. 介绍
对于文本编辑,qt提供了很多控件
- QLineEdit:单行文本输入,比如用户名密码等简单的较短的或者具有单一特征的字符串内容输入。使用text、settext读写
- QTextEdit:富文本编辑器,支持html显示,可以用sethtml/tohtml进行html文本操作或使用,也可利用setPlainText、toPlainText进行纯文本操作
- QPlainTextEdit:纯文本编辑器,使用了近似于textedit的技术并做了纯文本编辑的优化,并具有文章段落的概念也提供了撤销等功能,但不支持html显示。
- QTextBrowser:继承于QTextEdit,仅提供显示功能,并提供了超文本导航功能,如果不需要超文本连接只需要使用QTextEdit并设置QTextEdit::setReadOnly
上述都是显示控件,可以确定的是富文本编辑器要用QTextEdit或者QPlainTextEdit,但是肯定不能主动撰写html代码或者逐个处理显示格式实现富文本,实际上Qt提供了相关类:QTextDocument富文本文档、QTextBlock文本快、QTextFrame框架、QTextTable表格、QTextList列表、QTextCursor指针位置、QTextXXXXFormat各种数据类型样式。对于富文本的所有帮助请见官方文档:Rich Text Processing
QTextEdit和QPlainTextEdit选择:差异是QTextEdit提供了tohtml,如果想在处理完文档,直接根据文档生成html作为博客等内容,可以使用此类,没有需要后者即可
注意关系:QTextDocument>QTextFrame>QTextBlock/QTextTable/QTextList前包含后
查看两个类的api,均提供了document方法,可以返回QTextDocument指针,用于通过QTextDocument的方式操作文档内容格式,官方范例:
Application Example这个比较简单
Syntax Highlighter Example语法高亮的例子
Text Edit Example类似于word编辑器的例子
Calendar Example利用富文本编辑器的方式实现日历(不建议学这个毕竟已经有现成的日历控件,而且文档中往往也不会插入日历)
Order Form Example根据一些的参数设置生成报表,其实和上面的原理一样
2. 基本使用
首先当具有一个edit时,不需要自行创建document,可以直接用document方法可以获取当前edit的document
QTextDocument只是一个文档,其内还有根节点,需要使用QTextDocument::rootFlrame获取,设置根节点的边框粗细、颜色就类似于设置word文档边框底纹
2.1. 简单范例
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QTextDocument>
- #include <QTextFrame>
- #include <QTextBlock>
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow) {
- ui->setupUi(this);
- QTextDocument* doc = ui->textEdit->document();
- QTextFrame *root_frame = doc->rootFrame();
- QTextFrameFormat root_frame_format = root_frame->frameFormat();//创建框架格式
- root_frame_format.setBorderBrush(Qt::darkBlue);//设置边界颜色
- root_frame_format.setBorder(5);//设置边界宽度
- root_frame->setFrameFormat(root_frame_format); //给框架使用格式
- QTextFrameFormat frame_format;
- frame_format.setBackground(Qt::darkRed);//设置背景色
- frame_format.setMargin(10);//设置边距
- frame_format.setPadding(5);//设置填充
- frame_format.setBorder(2);//设置边界宽度
- frame_format.setBorderStyle(
- QTextFrameFormat::BorderStyle_Solid);//设置边框样式
- frame_format.setPosition(QTextFrameFormat::FloatRight);//右侧
- frame_format.setWidth(QTextLength(
- QTextLength::PercentageLength, 40));//宽度设置
- QTextCursor cursor = ui->textEdit->textCursor();
- cursor.insertText("A company");
- cursor.insertBlock();
- cursor.insertText("321 City Street");
- cursor.insertBlock();
- cursor.insertFrame(frame_format);
- cursor.insertText("Industry Park");
- cursor.insertBlock();
- cursor.insertText("Another country");
- }
上述代码仅显示了四行文字,前两行在root跟框架显示,后两行在一个新建的frame中显示,并将frame置于右侧限定了宽度,更多的布局方法请参考:Order Form Example
上述并未对文本格式做设置,可以在insertText的第二个参数直接赋予一个文本格式QTextCharFormat
2.2. QTextCursor光标操作/遍历嵌套Frame/遍历所有Block
首先他有各种instert函数可以插入上面提到的各种文档中的数据类型。有时并不能一次准确的建立好整个文档,需要在中间插入,这样就需要setPosition命令,而positon的具体值可以通过上述各种数据类型的类获取到每个块或者框架或者其他类型开头的positon,也可以通过length获取到当前块的长度用于定位末尾位置。下面提供一个简单范例
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QTextDocument>
- #include <QTextFrame>
- #include <QTextBlock>
- #include <QDebug>
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow) {
- ui->setupUi(this);
- QTextDocument* doc = ui->textEdit->document();
- QTextFrame *root_frame = doc->rootFrame();
- QTextFrameFormat root_frame_format = root_frame->frameFormat();//创建框架格式
- root_frame_format.setBorderBrush(Qt::darkBlue);//设置边界颜色
- root_frame_format.setBorder(5);//设置边界宽度
- root_frame->setFrameFormat(root_frame_format); //给框架使用格式
- QTextFrameFormat frame_format;
- frame_format.setBackground(Qt::darkRed);//设置背景色
- frame_format.setMargin(10);//设置边距
- frame_format.setPadding(5);//设置填充
- frame_format.setBorder(2);//设置边界宽度
- frame_format.setBorderStyle(
- QTextFrameFormat::BorderStyle_Solid);//设置边框样式
- frame_format.setPosition(QTextFrameFormat::FloatRight);//右侧
- frame_format.setWidth(QTextLength(
- QTextLength::PercentageLength, 40));//宽度设置
- QTextCursor cursor = ui->textEdit->textCursor();
- cursor.insertText("A company");
- cursor.insertBlock();
- cursor.insertText("321 City Street");
- cursor.insertFrame(frame_format);
- cursor.insertText("Industry Park");
- cursor.insertBlock();
- cursor.insertText("Another country");
- //遍历frame
- for(auto block = root_frame->begin();!block.atEnd();++block) {
- if(block.currentBlock().isValid()) {
- qDebug()<<block.currentBlock().text();
- }
- else if(block.currentFrame()) {//frame嵌套,范例只有两层所以不递归了
- auto child_frame = block.currentFrame();
- for(auto block2 = child_frame->begin();!block2.atEnd();++block2) {
- if(block.currentBlock().isValid()) {
- qDebug()<<block2.currentBlock().text();
- }
- }
- }
- }
- //还可以通过root_frame->childFrames()直接获取所字frame
- //遍历文本块
- QTextBlock block = doc->firstBlock();
- for(int i = 0; i < doc->blockCount();i++) {
- qDebug() << QString("block num:%1\tblock first line number:%2\tblock length:%3\ttext:")
- .arg(i).arg(block.firstLineNumber()).arg(block.length())
- << block.text();
- block = block.next();
- }
- QTextBlock insert_block = doc->firstBlock().next();
- //在第二行末尾添加
- cursor.setPosition(insert_block.position()+insert_block.length()-1);
- cursor.insertText("change cursor postion and insert");
- //在第三行开头添加-也就是新frame里面最开始添加
- //方法一,第二行末尾+1就是第三行开头
- cursor.setPosition(insert_block.position()+insert_block.length());
- //方法二,position默认返回的就是一个块开头
- cursor.setPosition(insert_block.next().position());
- //方法三,利用frame,frame是在一个锚点定位,开头在第二行末尾所以必须加一
- cursor.setPosition(frame_format.position()+1);
- cursor.insertText("change cursor postion and insert");
- }
前面的内容没有变化,后面先展示了如何遍历所有frame以及嵌套的frame。如果是全文检索或者整体修改也可以直接遍历全文所有block
Qt富文本编辑器QTextDocument的更多相关文章
- PyQt(Python+Qt)学习随笔:富文本编辑器QTextEdit功能详解
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.概述 QTextEdit是一个高级的所见即所得的文档查看器和编辑器 ...
- 在线富文本编辑器FckEditor配置(.Net Framework 3.5)
进入FCKeditor文件夹,编辑 fckconfig.js 文件.1.上传设置 . var _FileBrowserLanguage = 'php' ; // a ...
- 富文本编辑器Simditor的简易使用
最近打算自己做一个博客系统,并不打算使用帝国cms或者wordpress之类的做后台管理!自己处于学习阶段也就想把从前台到后台一起谢了.好了,废话不多说了,先来看看富文本编辑器SimDitor,这里是 ...
- 个人网站对xss跨站脚本攻击(重点是富文本编辑器情况)和sql注入攻击的防范
昨天本博客受到了xss跨站脚本注入攻击,3分钟攻陷--其实攻击者进攻的手法很简单,没啥技术含量.只能感叹自己之前竟然完全没防范. 这是数据库里留下的一些记录.最后那人弄了一个无限循环弹出框的脚本,估计 ...
- UEditor百度富文本编辑器--让编辑器自适应宽度的解决方案
UEditor百度富文本编辑器的initialFrameWidth属性,默认值是1000. 不能够自适应屏幕宽度.如图1: 刚开始的时候,我是直接设置initialFrameWidth=null的.效 ...
- PHP Ueditor 富文本编辑器
2016年12月11日 08:46:59 星期日 百度的简版富文本编辑器umeditor很久没更新了 全功能版本的配置项跟umeditor还是有区别的, 这里说下ueditor怎么对接到项目中去, 主 ...
- JavaScript 富文本编辑器
WEB项目中使用UEditor(富文本编辑器) UEditor - 完整示例 http://ueditor.baidu.com/website/onlinedemo.html UEditor注意事项: ...
- MVC 使用 Ueditor富文本编辑器
一.Ueditor 1.下载Ueditor富文本编辑器 官方下载地址: http://ueditor.baidu.com/website/download.html 建议下载开发版,此处我下载的是 . ...
- 富文本编辑器kindeditor配置
<!--富文本编辑器kindeditor配置↓ --> <link type="text/css" rel="stylesheet" href ...
随机推荐
- ECShop全系列版本远程代码执行漏洞复现
前言 问题发生在user.php的display函数,模版变量可控,导致注入,配合注入可达到远程代码执行 漏洞分析 0x01-SQL注入 先看user.php $back_act变量来源于HTTP_R ...
- 虚拟机内安装Centos7步骤
下面就来看看怎么安装centos7,首先就是要准备一个虚拟机了 简称VM,当然虚拟机的安装步骤,我也不再多说,我用的Workstation 15 Pro的版本,我们直接打开虚拟机,打开界面如下: 在安 ...
- VMWare 桥接模式
桥接网络模式是VMware虚拟机中最简单直接的模式. 桥接网络(Bridged Networking) 桥接网络是指本地物理网卡和虚拟网卡通过VMnet0虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图 ...
- 20155306 2016-2017-2 《Java程序设计》第十周学习总结
20155306 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 Java和Android开发学习(网络) 网络概览 计算机网络体系结构的通信协议划分为七层, ...
- 关于Linux_shell中的管道命令pipe “|”的简单学习和使用
什么是 "|"? |其实是linux shell 中的一个命令:管道命令(pipe) 管道命令操作符是:"|",它仅能处理经由前面一个指令传出的正确输出信息,也 ...
- 20155328 《Java程序设计》实验一(Java开发环境的熟悉) 实验报告
20155328 <Java程序设计>实验一(Java开发环境的熟悉) 实验报告 一.实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发: 打开windows ...
- 微信小程序解决地图上的层级关系
在有带地图的手机页面上,view无法显示在地图上方,所以,在wxml中,使用: <cover-view></cover-view> 能使view显示在地图上 注: 在该标签内部 ...
- 使用cursor递归遍历sqlserver的相应表
use rc GO )DECLARE cur1 cursor for select [name] from sys.tables where name LIKE 'index_%' drop tabl ...
- 简单的Slony-I设置实例
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页 参考如下链接: http://lets.postg ...
- 成都Uber优步司机奖励政策(4月18日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...