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. Vertical alignment in QTextEdit
Qt 6.11 is out! See what's new in the release blog

Vertical alignment in QTextEdit

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 3.5k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    David_Gil
    wrote on last edited by
    #1

    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!

    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