Vertical alignment in QTextEdit



  • Hi!

    I need to vertically align a table in a QTextEdit, but I can't find the way to do it. The table keeps staying at the top.

    Some code:

    @
    #include "mainwindow.h"
    #include <QTextEdit>
    #include <QTextCursor>
    #include <QTextTable>
    #include <QTextTableFormat>
    #include <QTextDocument>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    QString string = "Some text to check whether alignment is working or not.\n"
    "I only have to write some sentences.\n"
    "Long or short, it doesn't matter.";

    QTextEdit *editor = new QTextEdit(this);
    setCentralWidget(editor);
    resize(400,400);
    
    QTextDocument *document = new QTextDocument;
    editor->setDocument(document);
    
    QTextCursor cursor = editor->textCursor();
    cursor.movePosition(QTextCursor::Start);
    
    QTextTableFormat tableFormat;
    
    /*Qt::AlignCenter should align the table horizontally and vertically, but it only aligns horizontally.
    
    Other possibilities are:
    -Qt::AlignBottom
    -Qt::AlignVCenter
    but none of them aligns the table vertically*/
    
    tableFormat.setAlignment(Qt::AlignCenter);
    tableFormat.setWidth(QTextLength(QTextLength::PercentageLength,40));
    tableFormat.setBorder(1);
    
    QTextTable *table = cursor.insertTable(1,1,tableFormat);
    
    cursor = table->cellAt(0,0).firstCursorPosition();
    cursor.insertText(string);
    

    }
    @

    The simple header file:

    @
    #include <QMainWindow>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    MainWindow(QWidget *parent = 0);
    };
    @

    Thank you very much!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.