• QT QTextEdit富文本插入字体-表格-编号-图片-查找-语法高亮功能


    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)。
    框架将一个文档分为多个部分,在根框架里可以再添加文本块、子框架和表格等,一个文档的结构

    1. #ifndef MAINWINDOW5_3_H
    2. #define MAINWINDOW5_3_H
    3. #include
    4. class QLineEdit;
    5. class QDialog;
    6. class MySyntaxHiglighter;
    7. QT_BEGIN_NAMESPACE
    8. namespace Ui { class MainWindow5_3; }
    9. QT_END_NAMESPACE
    10. class MainWindow5_3 : public QMainWindow
    11. {
    12. Q_OBJECT
    13. public:
    14. MainWindow5_3(QWidget *parent = nullptr);
    15. ~MainWindow5_3();
    16. private:
    17. Ui::MainWindow5_3 *ui;
    18. QLineEdit *lineEdit; //对象指针
    19. QDialog *findDialog;
    20. MySyntaxHiglighter *my_highlighter;
    21. private slots:
    22. void showTextFrame();//获取文本框架
    23. void showTextBlock();//获取文本块
    24. void setTextFont(bool checked);
    25. void insertTable();
    26. void insertList();
    27. void insertImage();
    28. void textFind();
    29. void findNext();
    30. void findPrevious();
    31. };
    32. #endif // MAINWINDOW5_3_H
    1. #include "mainwindow5_3.h"
    2. #include "ui_mainwindow5_3.h"
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include "mysyntaxhiglighter.h"
    10. //编辑器中插入表格,列表,图片的方法
    11. MainWindow5_3::MainWindow5_3(QWidget *parent)
    12. : QMainWindow(parent)
    13. , ui(new Ui::MainWindow5_3)
    14. {
    15. ui->setupUi(this);
    16. QTextDocument *qDoc=ui->textEdit->document();//获取文档对象
    17. QTextFrame *root_frame = qDoc->rootFrame();//获取根框架
    18. QTextFrameFormat q_format;//创建框架的格式
    19. q_format.setBorderBrush(Qt::red);//边界的颜色
    20. q_format.setBorder(2);//边界线的宽度,粗细
    21. // root_frame->setFrameFormat(q_format);//框架使用格式
    22. QTextFrameFormat frameFormat;
    23. frameFormat.setBackground(Qt::darkGreen);
    24. frameFormat.setMargin(1);//边距
    25. frameFormat.setPadding(2);//填衬
    26. frameFormat.setBorder(2);
    27. frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted);//边框样式
    28. //QTextCursor cursor2=ui->textEdit->textCursor();//获取光标
    29. //cursor2.insertFrame(frameFormat);//在光标处插入框架
    30. //获取框架 QTextFrame操作
    31. QAction *action_textFrame=new QAction(tr("框架"),this);
    32. connect(action_textFrame,&QAction::triggered,this,&MainWindow5_3::showTextFrame);
    33. ui->toolBar->addAction(action_textFrame);
    34. //获取文本块 QTextBlock操作
    35. QAction *action_block= new QAction(tr("文本块"),this);
    36. connect(action_block,&QAction::triggered,this,&MainWindow5_3::showTextBlock);
    37. ui->toolBar->addAction(action_block);
    38. //设置字体
    39. QAction * action_font=new QAction(tr("字体"),this);
    40. action_font->setCheckable(true);
    41. //connect(action_font,&QAction::triggered,this,&MainWindow5_3::setTextFont);
    42. connect(action_font,&QAction::toggled,this,&MainWindow5_3::setTextFont);
    43. ui->toolBar->addAction(action_font);
    44. //
    45. //编辑器中插入表格,列表,图片的方法
    46. QAction *action_text_table=new QAction(tr("插入表格"),this);
    47. QAction *action_text_list=new QAction(tr("插入列表编号"),this);
    48. QAction *action_text_image=new QAction(tr("插入图片"),this);
    49. connect(action_text_table,&QAction::triggered,this,&MainWindow5_3::insertTable);
    50. connect(action_text_list,&QAction::triggered,this,&MainWindow5_3::insertList);
    51. connect(action_text_image,&QAction::triggered,this,&MainWindow5_3::insertImage);
    52. ui->toolBar->addAction(action_text_table);
    53. ui->toolBar->addAction(action_text_list);
    54. ui->toolBar->addAction(action_text_image);
    55. //实现查找功能
    56. QAction *action_text_find=new QAction(tr("查找"),this);
    57. connect(action_text_find,&QAction::triggered,this,&MainWindow5_3::textFind);//绑定工具栏的点击信号到执行函数
    58. ui->toolBar->addAction(action_text_find);
    59. findDialog=new QDialog(this);
    60. findDialog->setWindowTitle(tr("查找功能"));
    61. lineEdit = new QLineEdit(findDialog);//创建行编辑器
    62. QPushButton * q_pushbutton=new QPushButton(findDialog);//创建按钮
    63. // QPushButton * q_pushbutton_upstream=new QPushButton(findDialog);//创建按钮
    64. q_pushbutton->setText(tr("查找上一个"));
    65. // q_pushbutton_upstream->setText(tr("查找上一个"));
    66. connect(q_pushbutton,&QPushButton::clicked,this,&MainWindow5_3::findNext);
    67. // connect(q_pushbutton_upstream,&QPushButton::clicked,this,&MainWindow5_3::findPrevious);
    68. QVBoxLayout *qvbox_layout=new QVBoxLayout;//创建垂直布局管理器
    69. qvbox_layout->addWidget(lineEdit);
    70. qvbox_layout->addWidget(q_pushbutton);
    71. //qvbox_layout->addWidget(q_pushbutton_upstream);
    72. findDialog->setLayout(qvbox_layout);
    73. //语法高亮测试
    74. my_highlighter= new MySyntaxHiglighter(ui->textEdit->document());//创建MySyntaxHiglighter类对象
    75. //使用ui->textEdit->document()作为参数,文档改变将触发MySyntaxHiglighter的highlightBlock函数来设置语法高亮
    76. //QTextEdit 富文本 还支持HTML 子集
    77. ui->textEdit->append(tr("

      测试使用HTML方式

      "
      ));
    78. }
    79. MainWindow5_3::~MainWindow5_3()
    80. {
    81. delete ui;
    82. }
    83. //获取框架 QTextFrame操作
    84. void MainWindow5_3::showTextFrame()
    85. {
    86. QTextDocument *q_doc =ui->textEdit->document();
    87. QTextFrame *q_frame = q_doc->rootFrame();
    88. QTextFrame::iterator frame_iterator;
    89. for (frame_iterator=q_frame->begin();!(frame_iterator.atEnd());++frame_iterator)
    90. {
    91. QTextFrame *child_frame=frame_iterator.currentFrame();//获取当前框架的指针
    92. QTextBlock childBlock=frame_iterator.currentBlock();//获取当前文本块
    93. if(child_frame)
    94. {
    95. qDebug()<<"frame";
    96. }
    97. else if(childBlock.isValid())
    98. {
    99. qDebug()<<"block:"<text();
    100. }
    101. }
    102. }
    103. //获取文本块 QTextBlock操作
    104. void MainWindow5_3::showTextBlock()
    105. {
    106. QTextDocument *qdoc= ui->textEdit->document();
    107. QTextBlock qblock=qdoc->firstBlock();//获取文档的第一个文本块哦
    108. for(int i=0;iblockCount();i++)
    109. {
    110. qDebug()<<tr("文本块:%1,首行行号:%2,长度:%3,内容:").arg(i).arg(qblock.firstLineNumber()).arg(qblock.length())<text();
    111. qblock=qblock.next();
    112. }
    113. }
    114. void MainWindow5_3::setTextFont(bool checked)
    115. {
    116. if(checked)//处于选中状态
    117. {
    118. QTextCursor qcursor=ui->textEdit->textCursor();
    119. QTextBlockFormat block_format;//文本块格式
    120. block_format.setAlignment(Qt::AlignCenter);//水平居中
    121. qcursor.insertBlock(block_format);//插入文本块的格式
    122. QTextCharFormat char_format; //字符格式
    123. char_format.setBackground(Qt::blue);//背景色
    124. char_format.setForeground(Qt::yellow);//字体颜色
    125. char_format.setFont(QFont(tr("宋体"),12,QFont::Bold,true));//宋体12号,加粗,斜体字
    126. char_format.setFontUnderline(true);//使用下划线
    127. qcursor.setCharFormat(char_format);
    128. qcursor.insertText(tr("测试一下字体呢"));
    129. }
    130. else
    131. {
    132. qDebug()<<tr("字体未设置");
    133. }
    134. }
    135. void MainWindow5_3::insertTable()
    136. {
    137. QTextCursor qcursor=ui->textEdit->textCursor();
    138. QTextTableFormat qtext_table_format;
    139. qtext_table_format.setCellSpacing(2);
    140. qtext_table_format.setCellPadding(10);
    141. qcursor.insertTable(2,2,qtext_table_format);//插入2行2列
    142. }
    143. void MainWindow5_3::insertList()
    144. {
    145. QTextListFormat qtext_list_format;//列表格式
    146. qtext_list_format.setStyle(QTextListFormat::ListDecimal);//数字编码
    147. ui->textEdit->textCursor().insertList(qtext_list_format);
    148. }
    149. void MainWindow5_3::insertImage()//插入图片
    150. {
    151. QTextImageFormat qtext_image_format;
    152. //qtext_image_format.setName("../xxx/duck_dance.png");
    153. //qtext_image_format.setName("./duck_dance.png"); //图片放在debug的文件夹里面
    154. qtext_image_format.setName("duck_dance.png");//图片放在debug的文件夹里面
    155. ui->textEdit->textCursor().insertImage(qtext_image_format);
    156. }
    157. void MainWindow5_3::textFind()
    158. {
    159. findDialog->show();
    160. }
    161. void MainWindow5_3::findNext()
    162. {
    163. QString str2=lineEdit->text();
    164. //使用查找函数查找指定字符串,查找方式为向后查找
    165. // bool b_find=ui->textEdit->find(str2,QTextDocument::FindBackward);
    166. bool b_find=ui->textEdit->find(str2,QTextDocument::FindBackward);
    167. /*
    168. * 默认向前查找
    169. * FindBackward向后
    170. * FindCaseSensitively = 0x00002,不区分大小写
    171. FindWholeWords = 0x00004,匹配整个单词
    172. *
    173. */
    174. if(b_find)
    175. {
    176. qDebug()<<tr("行号:%1,列号:%2").arg(ui->textEdit->textCursor().blockNumber()).arg(ui->textEdit->textCursor().columnNumber());
    177. }
    178. else
    179. {
    180. qDebug()<<tr("未查找到信息");
    181. ui->textEdit->moveCursor(QTextCursor::End);//光标移动到句末尾
    182. //ui->textEdit->moveCursor(QTextCursor::Start);//光标移动到开始
    183. }
    184. }
    185. void MainWindow5_3::findPrevious()
    186. {
    187. QString str2=lineEdit->text();
    188. //使用查找函数查找指定字符串,查找方式为向后查找
    189. // bool b_find=ui->textEdit->find(str2,QTextDocument::FindBackward);
    190. bool b_find=ui->textEdit->find(str2,QTextDocument::FindBackward);
    191. /*
    192. * 默认向前查找
    193. * FindBackward向后
    194. * FindCaseSensitively = 0x00002,不区分大小写
    195. FindWholeWords = 0x00004,匹配整个单词
    196. *
    197. */
    198. if(b_find)
    199. {
    200. qDebug()<<tr("行号:%1,列号:%2").arg(ui->textEdit->textCursor().blockNumber()).arg(ui->textEdit->textCursor().columnNumber());
    201. }
    202. else
    203. {
    204. qDebug()<<tr("未查找到信息");
    205. }
    206. }

    语法高亮类文件

    1. #ifndef MYSYNTAXHIGLIGHTER_H
    2. #define MYSYNTAXHIGLIGHTER_H
    3. #include
    4. class MySyntaxHiglighter : public QSyntaxHighlighter
    5. {
    6. Q_OBJECT
    7. public:
    8. explicit MySyntaxHiglighter(QTextDocument *parent =0);
    9. protected:
    10. void highlightBlock(const QString &text) override;//重新实现该函数
    11. };
    12. #endif // MYSYNTAXHIGLIGHTER_H
    1. #include "mysyntaxhiglighter.h"
    2. MySyntaxHiglighter::MySyntaxHiglighter(QTextDocument *parent):QSyntaxHighlighter(parent)
    3. {
    4. }
    5. void MySyntaxHiglighter::highlightBlock(const QString &text)
    6. {
    7. QTextCharFormat my_format;
    8. my_format.setFontWeight(QFont::Bold);
    9. my_format.setForeground(Qt::blue);
    10. QString pattern2="\\bchar\\b";//匹配char
    11. QRegExp expression2(pattern2);
    12. int index2=text.indexOf(expression2);//从位置0开始匹配字符串
    13. while(index2>=0)
    14. {
    15. int length2=expression2.matchedLength();
    16. setFormat(index2,length2,my_format);//对要匹配的字符串设置格式
    17. index2=text.indexOf(expression2,index2+length2);//继续匹配
    18. }
    19. }

  • 相关阅读:
    SSM整合流程
    Android 使用Linphone SDK开发SIP客户端
    24、Flink 的table api与sql之Catalogs(java api操作分区与函数、表)-4
    八股文第十七天
    【docker】修改Docker默认存储位置修改配置文件方式
    操作系统—内核态和用户态
    Linux从0到1——Linux第一个小程序:进度条
    shiro基于cookie多服务器共享session,坑记录
    Java使用JavaMail进行邮件的发送和读取
    ETCD数据库源码分析——etcd gRPC 服务 API
  • 原文地址:https://blog.csdn.net/txwtech/article/details/126671120