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
    {
        Q_OBJECT
    
    public:
        MainWidget(QWidget *parent = 0);
        ~MainWidget();
    
    protected:
        void resizeEvent(QResizeEvent *event) override;
    
    public slots:
        void contextMenuRequested(const QPoint& pos);
        void showColorPicker();
    
    private:
        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)
    {
        setContextMenuPolicy(Qt::CustomContextMenu);
        connect(this, &QWidget::customContextMenuRequested,this, &MainWidget::contextMenuRequested);
    }
    
    MainWidget::~MainWidget()
    {
    
    }
    
    void MainWidget::contextMenuRequested(const QPoint& pos)
    {
        qDebug() << "Menu Requested";
        QMenu *contextMenu = new QMenu(this);
    
    
        QAction *action = contextMenu->addAction("Color");
    
        contextMenu->popup(mapToGlobal(pos));
    
        connect(action, &QAction::triggered, this, &MainWidget::showColorPicker);
    }
    
    void MainWidget::resizeEvent(QResizeEvent *event)
    {
        QTextEdit *te = new QTextEdit(this);
        QTextCursor cursor = te->textCursor();
    
        QTextTableFormat tf;
        tf.setBorderBrush(Qt::red);
        _table = cursor.insertTable(5, 5, tf);
    }
    
    void MainWidget::showColorPicker()
    {
        QColor color = QColorDialog::getColor();
    
        QTextTableFormat tf = _table->format();
        tf.setBorderBrush(color);
        _table->setFormat(tf);
    }
    

    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.