• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Qt富文本编辑器表格的操作

武飞扬头像
异想天开的余瑞银
帮助1

说明

原本计划中的方案时使用Qt的富文本编辑器做一个类Word的编辑器,配合OpenXML实现脱离office软件下对Word软件进行编辑的,但是该方案最终被放弃了,所以最终放到博客上做个记录。程序是在别人的开源代码上改的,原本的文字和图片的样式编辑已经有了,表格方面是我自己做的。

效果展示学新通

程序

插入表格

    QTextCursor cursor = ui->textEdit->textCursor();
       int row = 4;
       int col = 3;

       //通过光标插入表格
       QTextTable *table =  cursor.insertTable(row,col);
       //获取表格的格式
       QTextTableFormat tableFormat = table->format();
       //表格格式设置宽度
       tableFormat.setWidth(QTextLength(QTextLength::FixedLength,ui->textEdit->width()-60));
       //表格格式设置启用BorderCollapse
       //tableFormat.setBorderCollapse(true);

       //设置表格的columnWidthConstraints约束
       QVector<QTextLength> colLength = tableFormat.columnWidthConstraints();
       for (int i = 0; i < col;   i) {
           colLength.append(QTextLength(QTextLength::FixedLength,tableFormat.width().rawValue()/col));
       }
       colLength.clear();
       colLength.append(QTextLength(QTextLength::FixedLength,tableFormat.width().rawValue()/col-200));
       colLength.append(QTextLength(QTextLength::FixedLength,tableFormat.width().rawValue()/col 200));
       colLength.append(QTextLength(QTextLength::FixedLength,tableFormat.width().rawValue()/col));
       //QTextLength(QTextLength::FixedLength,tableFormat.width().rawValue()/col);

       tableFormat.setColumnWidthConstraints(colLength);
       tableFormat.setBorder(1);
       tableFormat.setBorderBrush(Qt::black);
       tableFormat.setAlignment(Qt::AlignCenter);
       //定义单元格格式
       QTextTableCellFormat cellFormat;
       cellFormat.setBackground(QColor("moccasin"));
       cellFormat.setVerticalAlignment( QTextCharFormat::AlignMiddle);
       QFont font("宋体");
       font.setPointSize(12);
       cellFormat.setFont(font);

       for (int i = 0; i < row;   i) {
           for (int j = 0; j < col;   j) {
               QTextTableCell cell = table->cellAt(i,j);
               if(i == 0)
               {
                   cell.setFormat(cellFormat);
               }
               QString str = QString::number(i * col   j   1);
               QTextCursor cellCursor = cell.firstCursorPosition();
               QTextBlockFormat cellBlockFormat = cellCursor.blockFormat();
               cellBlockFormat.setProperty(QTextFormat::BlockAlignment,Qt::AlignCenter);
               cellCursor.setBlockFormat(cellBlockFormat);
               cellCursor.insertText(str);
           }
       }
       table->setFormat(tableFormat);
学新通

文字及表格居左

单元格还是文字根据鼠标的选中情况

void TextEditor::on_action_Left_triggered()
{//左对齐

    QTextCursor cursor =ui->textEdit->textCursor();

    QTextTable* table = cursor.currentTable();
    if(table!=nullptr)
    {
    auto tableFormat =table->format();
    auto param = cursor.selectedText();
    if(param=="")
    {
    tableFormat.setAlignment(Qt::AlignLeft);
    table->setFormat(tableFormat);
    }
    else {
        ui->textEdit->setAlignment( Qt::AlignLeft );
    }

    }
    else {
        ui->textEdit->setAlignment( Qt::AlignLeft );
    }
    if(ui->action_Left->isChecked())//设置成单选
    {
            ui->action_Right->setChecked(false);
            ui->action_Center->setChecked(false);
    }
     else ui->action_Left->setChecked(true);
}
学新通

文字及表格居中

void TextEditor::on_action_Center_triggered()
{//居中

QTextCursor cursor =ui->textEdit->textCursor();

QTextTable* table = cursor.currentTable();

if(table!=nullptr)
{
int row = table->cellAt(cursor).row();
int column = table->cellAt(cursor).column();
qDebug()<<row<<column;
auto tableFormat =table->format();
auto param = cursor.selectedText();
if(param=="")
{
    tableFormat.setAlignment(Qt::AlignCenter);
    table->setFormat(tableFormat);
}
else
{
    ui->textEdit->setAlignment( Qt::AlignCenter );
}
}
else
{
    ui->textEdit->setAlignment( Qt::AlignCenter );
}

if(ui->action_Center->isChecked())//设置成单选
{
        ui->action_Just->setChecked(false);
        ui->action_Right->setChecked(false);
        ui->action_Left->setChecked(false);
}
else ui->action_Center->setChecked(true);
学新通

}

文字及表格居右

void TextEditor::on_action_Right_triggered()
{//右对齐

QTextCursor cursor =ui->textEdit->textCursor();

QTextTable* table = cursor.currentTable();
if(table!=nullptr)
{
auto tableFormat =table->format();
auto param = cursor.selectedText();
if(param=="")
{
tableFormat.setAlignment(Qt::AlignRight);
table->setFormat(tableFormat);
}
else
{
    ui->textEdit->setAlignment( Qt::AlignRight );
}

}
else
{
    ui->textEdit->setAlignment( Qt::AlignRight );
}
if(ui->action_Right->isChecked())//设置成单选
{
        ui->action_Left->setChecked(false);
        ui->action_Center->setChecked(false);
        ui->action_Just->setChecked(false);
}
else ui->action_Right->setChecked(true);
学新通

}

单元格合并

void TextEditor::on_action_mergeTableCell_triggered()       //将选中的单元格进行合并
{
    QTextCursor cursor =ui->textEdit->textCursor();
    if ( !cursor.hasSelection() )

           cursor.select( QTextCursor::WordUnderCursor );


    QTextTable* table = cursor.currentTable();
    if(table!=nullptr)
    {
     int firstRow,numRow,firstColumn,numColumn;
     cursor.selectedTableCells(&firstRow,&numRow,&firstColumn,&numColumn);
     table->mergeCells(firstRow,firstColumn,numRow,numColumn);
    }
}
学新通

单元格拆分

void TextEditor::on_action_splitTableCell_triggered()     //用于拆分合并后的单元格
{
    QTextCursor cursor =ui->textEdit->textCursor();
    if ( !cursor.hasSelection() )
    {
        cursor.select( QTextCursor::WordUnderCursor );
    }

    QTextTable* table = cursor.currentTable();
    if(table!=nullptr)
    {
    int row = table->cellAt(cursor).row();
    int column = table->cellAt(cursor).column();
    table->splitCell(row,column,1,1);

    }
}
学新通

最后

还有部分的单元格宽度调整背景颜色等功能还没有调试完成,将会在之后的博客中更新。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhibc
系列文章
更多 icon
同类精品
更多 icon
继续加载