Qt富文本编辑器中添加表格
说明
原本计划中的方案时使用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);
}
}
最后
还有部分的单元格宽度调整背景颜色等功能还没有调试完成,将会在之后的博客中更新。