Qt学习总结之QTextEdit

42 篇文章 1 订阅
36 篇文章 8 订阅

一.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_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    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
  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式小龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值