Important: Please read the Qt Code of Conduct -

QTextTable Issue

  • Hey,

    I'm trying to align text in the middle (vertical) of a QTextTableCell. The thing is it is working with the second cell, but not with the first one. The cause is the method setBlockFormat() or the used block format. What am I missing or is it a bug?

    Thanks in advance and best regards!
    Qt Version: 5.3.2

    Here's a minimal example:
    @#include "mainwindow.hpp"
    #include <QTextEdit>
    #include <QTextCursor>
    #include <QTextTableFormat>
    #include <QTextTableCell>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    QTextEdit *te = new QTextEdit(this);
    QTextCursor tc = te->textCursor();
    QTextTable *tt = tc.insertTable(2, 2);

    // cell width
    QTextTableFormat ttf = tt->format();
    QVector<QTextLength> cWC;
    cWC.append(QTextLength(QTextLength::PercentageLength, 70.0));
    cWC.append(QTextLength(QTextLength::PercentageLength, 30.0));
    QTextTableCell ttc = tt->cellAt(0, 0);
    // set cell height and block format alignment <- causing the offset
    QTextBlockFormat tbf = ttc.firstCursorPosition().blockFormat();
    tbf.setLineHeight(50.4f, QTextBlockFormat::FixedHeight);
    tbf.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    // set vertical alignment
    QTextCharFormat tcf = ttc.firstCursorPosition().charFormat();
    // set format for first column
    QTextCursor tc1 = tt->cellAt(0, 0).firstCursorPosition();
    tc1.setPosition(tt->cellAt(1, 0).firstPosition(), QTextCursor::KeepAnchor);
    tc1.setBlockFormat(tbf); // here is the cause, but whats wrong here?
    tc1.insertText("TEST"); // false alignment! seems to be one line off
    // set format for second column
    tc1 = tt->cellAt(0, 1).firstCursorPosition();
    tc1.setPosition(tt->cellAt(1, 1).firstPosition(), QTextCursor::KeepAnchor);
    tc1.insertText("TEST"); // correct




Log in to reply