• Qt学习总结之QTextEdit


    一.QTextEdit特性

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

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

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

    二.功能作用

    1.提示占位文本

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

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

    2.文本内容设置

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

    a.普通文本设定

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

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

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

    c.还可以自动设置文本

    m_edit->setText("zidong text");
    
    • 1

    d.其余API

    m_edit->append("添加文本");
    delay(2000);
    m_edit->clear();
    
    • 1
    • 2
    • 3

    三.文本光标

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

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

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

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

    1.插入文本

    m_edit->setFontFamily("黑体");        //设置字体格式
    m_edit->setFontPointSize(15);        //设置字体大小
    
    m_edit->append("插入文本");             //插入文本
    m_edit->insertHtml(" 百度");       //插入html文本(超链接)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.插入图片

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

    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);      //数字升序
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    创建之后,按回车即可,效果图如下:
    在这里插入图片描述
    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);                         //添加行
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

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

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

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

    QTextBlockFormat *mblockformat = new QTextBlockFormat();
    QTextCursor mcur = m_edit->textCursor();
    
    mblockformat->setIndent(2);
    
    QTextCharFormat *mcharformat = new QTextCharFormat();
    mcharformat->setFontPointSize(20);
    
    mcur.insertBlock(*mblockformat,*mcharformat);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    6.插入框架

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

    QTextCursor mcur = m_edit->textCursor();
    
    QTextFrameFormat mframe = QTextFrameFormat();
    
    mframe.setBorder(5);
    
    mcur.insertFrame(mframe);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    效果如下:
    在这里插入图片描述
    用文本框架可以把一个大的框架分出多个小框架

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    效果如下图所示:
    在这里插入图片描述

    源码:
    mainwindow:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include "windows.h"
    #include 
    #include 
    #include 
    #include 
    
    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(" 百度");       //插入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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    #include 
    #include 
    
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    springboot+音乐播放小程序 毕业设计-附源码191730
    Java IO包之File类简介说明
    MySQL数据库和表的操作
    TSINGSEE青犀车辆违停AI算法在园区道路管控场景中的应用方案
    二刷 K8s 源码 - workqueue 的所有细节
    最强Java面试八股文秋招offer召唤术
    【每日一题Day360】LC1465切割后面积最大的蛋糕 | 贪心
    如何使用 LeiaPix 让照片动起来
    H12-821_29
    SkeyeARS新版本发布,开启AR实景地图新篇章
  • 原文地址:https://blog.csdn.net/qq_27726087/article/details/126382052