'Application downing at editing QTextTable (Qt6)

Application downing at removing row with merging all columns in this row.

This is my testing code. How to test: Ctrl+T, select all cells in first row, Ctrl+M, Ctrl+R, click to table (may be 2-3 times). However, it is immediately clear that after deleting a row, the table layout is already destroyed. But first row must be empty. If the row is not empty, then there are no problems. What is my mistake?

#include "mainwindow.h"
#include <QAction>
#include <QTextEdit>
#include <QTextTable>

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{
    m_TextEdit = new QTextEdit(this);
    setCentralWidget(m_TextEdit);

    auto actionInsertTable = new QAction("Add table", this);
    actionInsertTable->setShortcut(Qt::CTRL | Qt::Key_T);
    QObject::connect(actionInsertTable, &QAction::triggered, this, &MainWindow::slotAddTable);

    auto actionMergeCells = new QAction("Merge cells", this);
    actionMergeCells->setShortcut(Qt::CTRL | Qt::Key_M);
    QObject::connect(actionMergeCells, &QAction::triggered, this, &MainWindow::slotMergeCells);

    auto actionRemoveRows = new QAction("Remove row", this);
    actionRemoveRows->setShortcut(Qt::CTRL | Qt::Key_R);
    QObject::connect(actionRemoveRows, &QAction::triggered, this, &MainWindow::slotRemoveRows);

    addAction(actionInsertTable);
    addAction(actionMergeCells);
    addAction(actionRemoveRows);
}

void MainWindow::slotAddTable()
{
    auto cursor = m_TextEdit->textCursor();
    auto t_col_count = 5;
    auto t_row_count = 5;

    QTextTableFormat ttf;
    ttf.setBackground(Qt::yellow);
    ttf.setAlignment(m_TextEdit->alignment());
    auto cltl = QList<QTextLength>();
    for (int i = 0; i < t_col_count; i++)
        cltl.append(QTextLength(QTextLength::PercentageLength, 100.0 / t_col_count));
    ttf.setColumnWidthConstraints(cltl);

    auto table = cursor.insertTable(t_row_count, t_col_count, ttf);
    auto cell = table->cellAt(0, 0);

    cursor = cell.firstCursorPosition();
    m_TextEdit->setTextCursor(cursor);
}

void MainWindow::slotMergeCells()
{
    auto cursor = m_TextEdit->textCursor();
    auto table = cursor.currentTable();

    if(!table) return;
    auto cell = table->cellAt(cursor);
    if(!cell.isValid()) return;

    table->mergeCells(cursor);
}

void MainWindow::slotRemoveRows()
{
    auto cursor = m_TextEdit->textCursor();
    auto table = cursor.currentTable();

    if(!table) return;
    auto cell = table->cellAt(cursor);
    if(!cell.isValid()) return;

    table->removeRows(cell.row(), 1);
}




Solution 1:[1]

Yes, it is probably necessary to update the columns format after row deleting.

auto ttf = table->format(); 
auto colcount = table->columns(); 
auto cltl = QList<QTextLength>(); 
for (int i = 0; i < colcount; i++)
cltl.append(QTextLength(QTextLength::PercentageLength, 100.0 / colcount));
ttf.setColumnWidthConstraints(cltl); 
table->setFormat(ttf); 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 avttrue