Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextTable Issue
QtWS25 Last Chance

QTextTable Issue

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 695 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saugglocke
    wrote on last edited by
    #1

    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));
    ttf.setColumnWidthConstraints(cWC);
    tt->setFormat(ttf);
    
    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();
    tcf.setVerticalAlignment(QTextCharFormat::AlignMiddle);
    
    // set format for first column
    QTextCursor tc1 = tt->cellAt(0, 0).firstCursorPosition();
    tc1.setPosition(tt->cellAt(1, 0).firstPosition(), QTextCursor::KeepAnchor);
    
    tc1.setBlockCharFormat(tcf);
    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.setBlockCharFormat(tcf);
    tc1.insertText("TEST"); // correct
    
    setCentralWidget(te);
    

    }

    MainWindow::~MainWindow()
    {

    }@

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved