一.QTextEdit特性

QTextEdit是一个高级的WYSIWYG(What You See Is What You Get所见即所得)编辑/查看器,支持使用HTML4标签子集的富文本格式。

QTextEdit它经过优化,可以处理大型文档并快速响应用户的输入,可以加载纯文本和富文本文件,用来显示图像、列表和表格。

QTextEdit的父类是QAbstractScrollArea,可以通过滚动条调整显示界面。

二.功能作用

1.提示占位文本

m_edit->setPlaceholderText("请输入...");      //设定占位文本

占位文本是控件的固有特性,在不改变的前提下是不会随着控件内文本的变化而改变的。

2.文本内容设置

由于QTextEdit是支持普通文本和html标签的,分别有两种文本的操作

a.普通文本设定

m_edit->setPlainText(“这是一个文本控件”); //普通文本设定
m_edit->insertPlainText(“+插入文本”); //光标处插入普通文本
m_edit->toPlainText(); //普通文本获取

b.html标签文本设定,用法和普通文本一样。

m_edit->setHtml("www.baidu.com");
m_edit->toHtml();

c.还可以自动设置文本

m_edit->setText("zidong text");

d.其余API

m_edit->append("添加文本");
delay(2000);
m_edit->clear();

三.文本光标

在上面的部分我们介绍了一种通过类提供的方法改变文本内容的方法,这里讲的是另一种:通过文本光标来操作文本框的内容。

首先来了解一下什么叫文本光标:通常我们在编辑文本文件时,是通过一个文本编辑器(就想word)操作的。word和文本文档在内存中建立了对应的关系,通过一个叫‘文本光标’的抽象的对象在内存中对文本文档进行操作。我们可以通过文本光标对QTextEdit进行各种操作。

获取光标,在获取光标后,就可以进行一系列的操作了。

QTextCursor cursor =  m_edit->textCursor(); //获取光标

1.插入文本

m_edit->setFontFamily("黑体");        //设置字体格式
m_edit->setFontPointSize(15);        //设置字体大小m_edit->append("插入文本");             //插入文本
m_edit->insertHtml("<a href='http://www.baidu.com'> 百度</a>");       //插入html文本(超链接)

2.插入图片

QTextCursor m_cur = m_edit->textCursor();
QTextImageFormat imageFormat;   // 保存图片格式对象
imageFormat.setName("image//1.jpeg");       //设置图片路径
imageFormat.setHeight(200);                 //设置图片高度
imageFormat.setWidth(200);                  //设置图片宽度m_cur.insertImage(imageFormat);             //在当前图标下添加图片

3.插入列表

还可以在QTextEdit里插入一个列表

//    QTextListFormat.ListDisc       //圆点
//    QTextListFormat.ListCircle     //空心圆
//    QTextListFormat.ListSquare     //方块
//    QTextListFormat.ListDecimal    //数字升序
//    QTextListFormat.ListUpperRoman //大写罗马数字
//    QTextListFormat.ListLowerRoman //小写罗马数字
//    QTextListFormat.ListUpperAlpha //大写拉丁字母
//    QTextListFormat.ListLowerAlpha //小写拉丁字母QTextCursor m_cur = m_edit->textCursor();           //获取当前光标m_cur.insertList(QTextListFormat::ListCircle);  //空心圆
m_cur.insertList(QTextListFormat::ListDisc);        //圆点
m_cur.insertList(QTextListFormat::ListSquare);      //数字升序

创建之后,按回车即可,效果图如下:

4.插入表格

可以在文本块内插入表格

QTextTableFormat *mtableformat = new QTextTableFormat();
QTextCursor mcur = m_edit->textCursor();mtableformat->setCellPadding(10);             //单元格内文本和边框距离
mtableformat->setCellSpacing(10);             //单元格线宽
mtableformat->setAlignment(Qt::AlignCenter);  //对齐模式QTextTable *m_table = mcur.insertTable(3,5,*mtableformat);      //创建表格
delay(5000);m_table->appendColumns(2);                      //添加列
delay(5000);
m_table->appendRows(2);                         //添加行

效果图如下:

5.插入文本块
这里说的文本块就是一个用回车键分隔的段落(block),是可以带有一定格式或样式的。插入文本块有这三种方法

insertBlock()                                #插入空文本块
insertBlock(QTextBlockFormat)                #插入文本块的同时设置文本块格式
insertBlock(QTextBlockFormat,QTextCharFormat)#插入文本块同时设置文本块格式和字符格式

注意的是第三种,要明白文本块格式和字符格式的区别:由于文本块是一个段落,就会有缩进、对齐方式、间距等,而单个的字符格式就是字体、颜色、背景色等等。

QTextBlockFormat *mblockformat = new QTextBlockFormat();
QTextCursor mcur = m_edit->textCursor();mblockformat->setIndent(2);QTextCharFormat *mcharformat = new QTextCharFormat();
mcharformat->setFontPointSize(20);mcur.insertBlock(*mblockformat,*mcharformat);

6.插入框架

在文本框内还可以插入一个框架,在文本框内的框架里还可以输入字符,用法是这样的

QTextCursor mcur = m_edit->textCursor();QTextFrameFormat mframe = QTextFrameFormat();mframe.setBorder(5);mcur.insertFrame(mframe);

效果如下:

用文本框架可以把一个大的框架分出多个小框架

QTextCursor mcur = m_edit->textCursor();QTextFrameFormat mframe = QTextFrameFormat();mframe.setBorder(5);
mframe.setBorderBrush(QColor(50,50,50));//    mcur.insertFrame(mframe);QTextDocument *mdoc = m_edit->document();
QTextFrame *root_frame = mdoc->rootFrame();
root_frame->setFrameFormat(mframe);mcur.insertFrame(mframe);

效果如下图所示:

源码:
mainwindow:

#include "mainwindow.h"
#include "ui_mainwindow.h"#include "windows.h"
#include <QTimer>
#include <QPushButton>
#include <QTextList>
#include <QTextTable>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);this->resize(800,600);m_edit = new QTextEdit(this);m_edit->setGeometry(50,50,600,400);int hcnt = 50;int hinsert = 40;mbt = new QPushButton("添加文本",this);mbt->setGeometry(660,hcnt,100,20);hcnt+=hinsert;bt_addpic = new QPushButton("添加图片",this);bt_addpic->setGeometry(660,hcnt,100,20);hcnt+=hinsert;bt_addlist1 = new QPushButton("添加列表1",this);bt_addlist1->setGeometry(660,hcnt,100,20);hcnt+=hinsert;bt_addlist2 = new QPushButton("添加列表2",this);bt_addlist2->setGeometry(660,hcnt,100,20);hcnt+=hinsert;bt_addlist3 = new QPushButton("添加列表3",this);bt_addlist3->setGeometry(660,hcnt,100,20);hcnt+=hinsert;bt_addtable = new QPushButton("添加表格",this);bt_addtable->setGeometry(660,hcnt,100,20);hcnt+=hinsert;bt_addtextblock = new QPushButton("添加文本块",this);bt_addtextblock->setGeometry(660,hcnt,100,20);hcnt+=hinsert;bt_addframe = new QPushButton("添加框架",this);bt_addframe->setGeometry(660,hcnt,100,20);hcnt+=hinsert;bt_clear = new QPushButton("清空",this);bt_clear->setGeometry(660,hcnt,100,20);connect(mbt,&QPushButton::clicked,this,&MainWindow::slot_mbt);connect(bt_addpic,&QPushButton::clicked,this,&MainWindow::slot_bt_addpic);connect(bt_addlist1,&QPushButton::clicked,this,&MainWindow::slot_bt_addlist1);connect(bt_addlist2,&QPushButton::clicked,this,&MainWindow::slot_bt_addlist2);connect(bt_addlist3,&QPushButton::clicked,this,&MainWindow::slot_bt_addlist3);connect(bt_addtable,&QPushButton::clicked,this,&MainWindow::slot_addtable);connect(bt_addtextblock,&QPushButton::clicked,this,&MainWindow::slot_addtextblock);connect(bt_addframe,&QPushButton::clicked,this,&MainWindow::slot_addframe);connect(bt_clear,&QPushButton::clicked,this,&MainWindow::slot_clear);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::delay(int msec)
{   // 这个最准/*非阻塞方式延时,现在很多人推荐的方法*/QEventLoop loop;QTimer::singleShot(msec, &loop, SLOT(quit()));loop.exec();
}void  MainWindow::process()
{m_edit->setPlaceholderText("请输入...");      //设定占位文本//    m_edit->setPlainText("这是一个文本控件");        //普通文本设定
//    m_edit->insertPlainText("+插入文本");          //光标处插入普通文本
//    m_edit->toPlainText();                      //普通文本获取m_edit->setHtml("www.baidu.com");m_edit->toHtml();m_edit->setText("zidong text");m_edit->append("添加文本");delay(20);m_edit->clear();QTextCursor cursor =  m_edit->textCursor(); //获取光标//    mbt->move(600,550);
}void MainWindow::slot_mbt()
{m_edit->setFontFamily("黑体");        //设置字体格式m_edit->setFontPointSize(15);        //设置字体大小m_edit->append("插入文本");             //插入文本m_edit->insertHtml("<a href='http://www.baidu.com'> 百度</a>");       //插入html文本(超链接)
}void MainWindow::slot_bt_addpic()
{QTextCursor m_cur = m_edit->textCursor();QTextImageFormat imageFormat;   // 保存图片格式对象imageFormat.setName("image//1.jpeg");       //设置图片路径imageFormat.setHeight(200);                 //设置图片高度imageFormat.setWidth(200);                  //设置图片宽度m_cur.insertImage(imageFormat);             //在当前图标下添加图片
}void MainWindow::slot_bt_addlist1()
{QTextListFormat *list1 = new QTextListFormat();QTextCursor m_cur = m_edit->textCursor();m_cur.insertList(QTextListFormat::ListCircle);  //空心圆
//    m_cur.insertList(list1);//    QTextListFormat.ListDisc       //圆点
//    QTextListFormat.ListCircle     //空心圆
//    QTextListFormat.ListSquare     //方块
//    QTextListFormat.ListDecimal    //数字升序
//    QTextListFormat.ListUpperRoman //大写罗马数字
//    QTextListFormat.ListLowerRoman //小写罗马数字
//    QTextListFormat.ListUpperAlpha //大写拉丁字母
//    QTextListFormat.ListLowerAlpha //小写拉丁字母
}QTextListFormat *list1 = new QTextListFormat();void MainWindow::slot_bt_addlist2()
{//    QTextListFormat *list1 = new QTextListFormat();QTextCursor m_cur = m_edit->textCursor();           //获取当前光标m_cur.insertList(QTextListFormat::ListDisc);        //圆点
}void MainWindow::slot_bt_addlist3()
{//    QTextListFormat *list1 = new QTextListFormat();QTextCursor m_cur = m_edit->textCursor();m_cur.insertList(QTextListFormat::ListDecimal);      //数字升序}void MainWindow::slot_addtable()
{QTextTableFormat *mtableformat = new QTextTableFormat();QTextCursor mcur = m_edit->textCursor();mtableformat->setCellPadding(10);             //单元格内文本和边框距离mtableformat->setCellSpacing(10);             //单元格线宽mtableformat->setAlignment(Qt::AlignCenter);  //对齐模式QTextTable *m_table = mcur.insertTable(3,5,*mtableformat);      //创建表格delay(5000);m_table->appendColumns(2);                      //添加列delay(5000);m_table->appendRows(2);                         //添加行
}void MainWindow::slot_addtextblock()
{QTextBlockFormat *mblockformat = new QTextBlockFormat();QTextCursor mcur = m_edit->textCursor();mblockformat->setIndent(2);QTextCharFormat *mcharformat = new QTextCharFormat();mcharformat->setFontPointSize(20);mcur.insertBlock(*mblockformat,*mcharformat);
}void MainWindow::slot_addframe()
{QTextCursor mcur = m_edit->textCursor();QTextFrameFormat mframe = QTextFrameFormat();mframe.setBorder(5);mframe.setBorderBrush(QColor(50,50,50));//    mcur.insertFrame(mframe);QTextDocument *mdoc = m_edit->document();QTextFrame *root_frame = mdoc->rootFrame();root_frame->setFrameFormat(mframe);mcur.insertFrame(mframe);
}void MainWindow::slot_clear()
{m_edit->clear();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTextEdit>
#include <QPushButton>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void process();
private slots:void slot_mbt();void slot_bt_addpic();void slot_bt_addlist1();void slot_bt_addlist2();void slot_bt_addlist3();void slot_addtable();void slot_addtextblock();void slot_addframe();void slot_clear();
private:Ui::MainWindow *ui;QTextEdit *m_edit;void delay(int msec);QPushButton *mbt;QPushButton *bt_addpic;QPushButton *bt_addlist1;QPushButton *bt_addlist2;QPushButton *bt_addlist3;QPushButton *bt_addtable;QPushButton *bt_addtextblock;QPushButton *bt_addframe;QPushButton *bt_clear;};
#endif // MAINWINDOW_H

Qt学习总结之QTextEdit相关推荐

  1. 【QT学习六】QTextEdit

    目录 一.概述 二.QTextEdit的使用 创建QTextEdit控件 设置文本内容 获取文本内容 格式化文本 设置文本格式 插入图像 插入超链接 其他常用函数 三.QTextEdit类使用时注意事 ...

  2. QT学习笔记(六):Qt5主窗口框架示例

    QT学习笔记(五):Qt5主窗口框架代码示例 一.添加编辑菜单:并在下拉菜单和工具栏中添加"打开文件"动作菜单 #include <QToolButton> #incl ...

  3. QT学习笔记(摘抄)

    QT学习笔记-1.QT主要的对象 说来惭愧学习c++很长时间了一直没有使用c++开发过软件界面 所以现在想认认真真的学习一个c++图形界面框架库 本来想学习Xwidget但是这个资料不大好找 有啥问题 ...

  4. Qt学习笔记,Qt国际化

    Qt学习笔记,Qt国际化 Qt国际化步骤: 第一步:设置.pro文件,加入TRANSLATIONS为国际化做准备 TRANSLATIONS = language/language_en.ts\     ...

  5. Qt学习笔记,Qt程序架构设计要旨

    Qt学习笔记,Qt程序架构设计要旨 时间过得很快,转眼学习Qt已经有一个多月了,对Qt的学习也在不断的深入中.自己手下的code也很多了,不过不得不说,还有很多的部分没有接触过,比如网络编程,2D,3 ...

  6. 转载: Qt 学习之路 2归档

    Qt 学习之路 2归档 http://www.devbean.net/2012/08/qt-study-road-2-catelog/

  7. 对QT学习之路12-14的源代码补充与修正

    QT学习之路12-14的源代码有些不完整,为了更好的让大家学习,本人做了一点修正与补充,谢谢.源代码如下: 头文件: #ifndef MAINWINDOW_H #define MAINWINDOW_H ...

  8. Qt学习笔记之MySQL数据库

    一.MySQL概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQ ...

  9. Qt学习笔记之数据库

    一.数据库简介 1.1.数据和数据库(DB) 用计算机进行数据处理,首先就要把信息以数据形式存储到计算机中,故数据是可以被计算机接受和处理的符号.根据所表示的信息特征不同,数据有不同的类别,如数字.文 ...

最新文章

  1. 【Go】Go基础(七):包
  2. Java对多线程的支持
  3. hall's marriage theorem
  4. iframe 的一点经历
  5. jdbc_servlet基础增删改分页2(userinfo表的)
  6. Java中的方法调用有多昂贵
  7. Spring Boot学习总结(2)——Spring Boot整合Jsp
  8. 2.4 sklearn中的metrics.roc_auc_score评价指标
  9. OFFICE | WORD VBA 合集
  10. 计算机的内存大小有何作用,电脑内存用处有多大?你可能想不到!
  11. OpenCV视频处理操作
  12. Python之sep与end
  13. 歌曲排行榜html页面,音乐组合排行板_音乐排行榜网页模板
  14. java 输入地址_Java输入邮件地址
  15. Windows查看进程命令
  16. MVC 音乐商店 第1部分: 概述和文件- 新建项目
  17. Opencv 特征训练分类器
  18. Jetpack Navigation 真香预警
  19. phpexcel 读取数据
  20. 康托展开(基于全排列的某一种hash)

热门文章

  1. 为什么要新零售 新零售的“人、货、场”需要哪些变化?
  2. 【Notepad++】notepad++主题和字体设置(非常好看舒服的)
  3. 收集各种在线HTTP网站载入速度(响应时间)站长测试(检测)工具
  4. IRFR3704Z替代兼容型号DC005NG-I,完美替换IRFR3704Z,封装TO-252
  5. 2018年6月18日训练日记
  6. ESP8266与网络调试助手的通信(TCP ServerTCP Client)
  7. 什么是位和字节,字和半字
  8. Android 视频播放器JieCaoVideoPlayer使用(防今日头条视频预加载)
  9. uniapp相册选择页国际化问题
  10. linux at命令无效,linux at命令