Chaning border color from QTextTable with QColor-Dialog

  • Hi. How can i Change the Color of QTextTable border with a Color Dialog? I know that i can Change it with QBrush and QTextTableFormat but how can i do this with a Color Dialog?

  • Hi,

    If you want to pick a color from a Color Dialog, just use the static method getColor in <QColorDialog>

    QColor color = QColorDialog::getColor();

    Then you can use this color to change your QTextTAbleFormat.

    see you!

  • Hi Henrik,

    as you requested by chat this is a more deep example.

    #ifndef MAINWIDGET_H
    #define MAINWIDGET_H
    #include <QWidget>
    class QResizeEvent;
    class QTextTable;
    class MainWidget : public QWidget
        MainWidget(QWidget *parent = 0);
        void resizeEvent(QResizeEvent *event) override;
    public slots:
        void contextMenuRequested(const QPoint& pos);
        void showColorPicker();
        QTextTable *_table;
    #endif // MAINWIDGET_H
    #include "MainWidget.h"
    #include <QTextEdit>
    #include <QTextCursor>
    #include <QTextTable>
    #include <QMenu>
    #include <QAction>
    #include <QColorDialog>
    #include <QDebug>
    MainWidget::MainWidget(QWidget *parent)
        : QWidget(parent)
        connect(this, &QWidget::customContextMenuRequested,this, &MainWidget::contextMenuRequested);
    void MainWidget::contextMenuRequested(const QPoint& pos)
        qDebug() << "Menu Requested";
        QMenu *contextMenu = new QMenu(this);
        QAction *action = contextMenu->addAction("Color");
        connect(action, &QAction::triggered, this, &MainWidget::showColorPicker);
    void MainWidget::resizeEvent(QResizeEvent *event)
        QTextEdit *te = new QTextEdit(this);
        QTextCursor cursor = te->textCursor();
        QTextTableFormat tf;
        _table = cursor.insertTable(5, 5, tf);
    void MainWidget::showColorPicker()
        QColor color = QColorDialog::getColor();
        QTextTableFormat tf = _table->format();

    Maybe you could change the QTextDocument style, to change all the tables in the document, this is somenthing I don't know.

    Hope this helps!

Log in to reply

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