If you want to align when inserting text, you can call this function with alignment = Qt: AlignHCenter. You can change the function to also indicate character formatting.
void insertAlignedText(QTextTable *table, int row, int col, Qt::Alignment alignment, QString text)
{
QTextCursor textCursor = table->cellAt(row,col).firstCursorPosition();
QTextBlockFormat blockFormat = textCursor.blockFormat();
Qt::Alignment vertAlign = blockFormat.alignment() & Qt::AlignVertical_Mask;
Qt::Alignment horzAlign = alignment & Qt::AlignHorizontal_Mask;
Qt::Alignment combAlign = horzAlign | vertAlign;
blockFormat.setAlignment(combAlign);
textCursor.setBlockFormat(blockFormat);
textCursor.insertText(text);
}
source
share