Color Picker in QT



  • I have a requirement in which I have Right Mouse Button and menu comes Color . When the user clicks on Color pop up , When the user clicks on Color pop up , a color picker dialog should come up and when the user picks the color and presses OK then the a rectangle in other QGraphicsView should=be filled with the selected color

    I assume I should QColorDialog should work



  • @Qt-Enthusiast said:

    I have a requirement in which I have Right Mouse Button and menu comes Color . When the user clicks on Color pop up , When the user clicks on Color pop up , a color picker dialog should come up and when the user picks the color and presses OK then the a rectangle in other QGraphicsView should=be filled with the selected color

    I assume I should QColorDialog should work

    I have a requirement in which I have Right Mouse Button and menu comes Color . When the user clicks on Color pop up , When the user clicks on Color pop up , a color picker dialog should come up and when the user picks the color and presses OK then the a rectangle in other QGraphicsView should=be filled with the selected color

    I assume I should QColorDialog should work. Please let me know if I am going correct and

    It will be helpful I get ar reference example Where I come to know how to use QColorDialog



  • Hi,

    Not sure if I understood exactly what you wanted, I created a context menu when right clicking over your scene with the action "color" and when clicking the action, the color dialog will show up.

    This is the header code:

    #ifndef MAINWIDGET_H
    #define MAINWIDGET_H
    
    #include <QWidget>
    
    class QGraphicsRectItem;
    
    class MainWidget : public QWidget
    {
        Q_OBJECT
    
    public:
        MainWidget(QWidget *parent = 0);
        ~MainWidget();
    
    public slots:
        void contextMenuRequested(const QPoint& pos);
        void showColorPicker();
    
    protected:
        virtual void resizeEvent(QResizeEvent *event) override;
    
        QGraphicsRectItem *m_rect;
    };
    
    #endif // MAINWIDGET_H
    
    

    This is the cpp

    #include "MainWidget.h"
    
    #include <QGraphicsView>
    #include <QGraphicsWidget>
    #include <QMenu>
    #include <QDebug>
    #include <QAction>
    #include <QGraphicsRectItem>
    #include <QColorDialog>
    
    MainWidget::MainWidget(QWidget *parent)
        : QWidget(parent)
    {
        setContextMenuPolicy(Qt::CustomContextMenu);
        connect(this, &QWidget::customContextMenuRequested,this, &MainWidget::contextMenuRequested);
    }
    
    MainWidget::~MainWidget()
    {
    
    }
    
    void MainWidget::resizeEvent(QResizeEvent *event)
    {
        Q_UNUSED(event);
        QGraphicsView *gv = new QGraphicsView(this);
        QGraphicsScene *gs = new QGraphicsScene(this);
    
        gv->setScene(gs);
        m_rect = gs->addRect(10, 10, 500, 500, QPen(Qt::blue), QBrush(Qt::red));
    }
    
    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::showColorPicker()
    {
        QColor color = QColorDialog::getColor();
        m_rect->setBrush(color);
    }
    

    This is a very basic example working ok for me. You'll have to work a little bit around it.

    Hope it helps!


Log in to reply
 

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