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. Color Picker in QT
Qt 6.11 is out! See what's new in the release blog

Color Picker in QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.3k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      Qt Enthusiast
      wrote on last edited by
      #2

      @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

      1 Reply Last reply
      0
      • X Offline
        X Offline
        XavierLL
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved