QTextEdit 插入Html 表格(table)

来源:互联网 发布:mac上的杀毒软件 编辑:程序博客网 时间:2024/05/22 00:25

业务需求:想要实现下图效果:
这里写图片描述

然而直接使用html代码:

<table  style="width: 124px; height: 88px;"  border="1">      <tbody>        <tr>          <td  style="background-color: rgb(255, 255, 102);">A</td>          <td>1</td>        </tr>        <tr>          <td  style="background-color: rgb(51, 204, 255); width: 126.8px;">B</td>          <td  style="width: 411.2px;">2</td>        </tr>        <tr>          <td  style="background-color: rgb(153, 51, 153);">C</td>          <td>3</td>        </tr>      </tbody>    </table>

在QTextEdit中却没有效果,主要是没有边框。
经过搜索,找到QTextTable可以在QTextEdit中插入表格,QTextList用于插入列表。
更多内容可参考帮助:Rich Text Processing

具体实现:(忽略颜色)

    QTextCursor cursor(this->ui->_textEdit->textCursor());    cursor.movePosition(QTextCursor::Start);    QTextTable _table=cursor.insertTable(3,2);    _table.cellAt(0,0).firstCursorPosition().insertHtml("A");    _table.cellAt(0,1).firstCursorPosition().insertHtml("1");    _table.cellAt(1,0).firstCursorPosition().insertHtml("B");    _table.cellAt(1,1).firstCursorPosition().insertHtml("2");    _table.cellAt(2,0).firstCursorPosition().insertHtml("C");    _table.cellAt(2,1).firstCursorPosition().insertHtml("3");

另外要说的是,表格的分割和合并。
比如要把上图中数字2的位置分割成三行,想通过splitCell直接分割是不能实现的。
能做的只能是先插入一个5行2列的表格,然后把中间三个的第一个单元格合并。

原创粉丝点击