QtColorPicker Class Reference

The QtColorPicker class provides a widget for selecting colors from a popup color grid. More...

#include <qtcolorpicker.h>

Public Slots

void setCurrentColor (const QColor &col)


void colorChanged (const QColor &)
void colorSet (const QColor &)

Public Member Functions

QColor color (int index) const
bool colorDialogEnabled () const
QColor currentColor () const
void insertColor (const QColor &color, const QString &text=QString(), int index=-1)
 QtColorPicker (QWidget *parent=nullptr, int columns=-1, bool enableColorDialog=true)
void setColorDialogEnabled (bool enabled)
void setStandardColors ()
 ~QtColorPicker ()

Static Public Member Functions

static QColor getColor (const QPoint &pos, bool allowCustomColors=true)

Protected Member Functions

void paintEvent (QPaintEvent *e)


bool colorDialog
 Whether the ellipsis "..." (more) button is available. More...

Detailed Description

The QtColorPicker class provides a widget for selecting colors from a popup color grid.

Users can invoke the color picker by clicking on it, or by navigating to it and pressing Space. They can use the mouse or arrow keys to navigate between colors on the grid, and select a color by clicking or by pressing Enter or Space. The colorChanged() signal is emitted whenever the color picker's color changes.

The widget also supports negative selection: Users can click and hold the mouse button on the QtColorPicker widget, then move the mouse over the color grid and release the mouse button over the color they wish to select.

The color grid shows a customized selection of colors. An optional ellipsis "..." button (signifying "more") can be added at the bottom of the grid; if the user presses this, a QColorDialog pops up and lets them choose any color they like. This button is made available by using setColorDialogEnabled().

When a color is selected, the QtColorPicker widget shows the color and its name. If the name cannot be determined, the translatable name "Custom" is used.

The QtColorPicker object is optionally initialized with the number of columns in the color grid. Colors are then added left to right, top to bottom using insertColor(). If the number of columns is not set, QtColorPicker calculates the number of columns and rows that will make the grid as square as possible.

DrawWidget::DrawWidget(QWidget *parent, const char *name)
QtColorPicker *picker = new QtColorPicker(this);
picker->insertColor(red, "Red"));
picker->insertColor(QColor("green"), "Green"));
picker->insertColor(QColor(0, 0, 255), "Blue"));
connect(colors, SIGNAL(colorChanged(const QColor &)), SLOT(setCurrentColor(const QColor &)));

An alternative to adding colors manually is to initialize the grid with QColorDialog's standard colors using setStandardColors().

QtColorPicker also provides a the static function getColor(), which pops up the grid of standard colors at any given point.

\img colorpicker1.png \img colorpicker2.png

See also

Constructor & Destructor Documentation

◆ QtColorPicker()

QtColorPicker::QtColorPicker ( QWidget parent = nullptr,
int  cols = -1,
bool  enableColorDialog = true 

Constructs a QtColorPicker widget. The popup will display a grid with cols columns, or if cols is -1, the number of columns will be calculated automatically.

If enableColorDialog is true, the popup will also have a "More" button (signified by an ellipsis "...") that presents a QColorDialog when clicked.

After constructing a QtColorPicker, call insertColor() to add individual colors to the popup grid, or call setStandardColors() to add all the standard colors in one go.

The parent argument is passed to QFrame's constructor.

See also

References draftgeoutils.intersections::connect(), and setCurrentColor().

◆ ~QtColorPicker()

QtColorPicker::~QtColorPicker ( )

Destructs the QtColorPicker.

Member Function Documentation

◆ color()

◆ colorChanged

QtColorPicker::colorChanged ( const QColor &  color)

This signal is emitted when the QtColorPicker's color is changed. color is the new color.

To obtain the color's name, use text().

Referenced by setCurrentColor().

◆ colorDialogEnabled()

bool QtColorPicker::colorDialogEnabled ( ) const

◆ colorSet

void QtColorPicker::colorSet ( const QColor &  )

Referenced by setCurrentColor().

◆ currentColor()

QColor QtColorPicker::currentColor ( ) const

Returns the currently selected color.

See also

◆ getColor()

QColor QtColorPicker::getColor ( const QPoint &  point,
bool  allowCustomColors = true 

Pops up a color grid with Qt default colors at point, using global coordinates. If allowCustomColors is true, there will also be a button on the popup that invokes QColorDialog.

For example:

void Drawer::mouseReleaseEvent(QMouseEvent *e)
if (e->button() & RightButton) {
QColor color = QtColorPicker::getColor(mapToGlobal(e->pos()));

References ColorPickerPopup::exec(), ColorPickerPopup::insertColor(), and ColorPickerPopup::lastSelected().

Referenced by draftguitools.gui_setstyle.Draft_SetStyle_TaskPanel::getPrefColor(), and draftguitools.gui_setstyle.Draft_SetStyle_TaskPanel::setValues().

◆ insertColor()

void QtColorPicker::insertColor ( const QColor &  color,
const QString &  text = QString(),
int  index = -1 

Adds the color color with the name text to the color grid, at position index. If index is -1, the color is assigned automatically assigned a position, starting from left to right, top to bottom.

References color(), and ColorPickerPopup::insertColor().

Referenced by setCurrentColor(), and setStandardColors().

◆ paintEvent()

void QtColorPicker::paintEvent ( QPaintEvent *  e)

References color().

◆ setColorDialogEnabled()

void QtColorPicker::setColorDialogEnabled ( bool  enabled)

◆ setCurrentColor

void QtColorPicker::setCurrentColor ( const QColor &  color)

Makes color current. If color is not already in the color grid, it is inserted with the text "Custom".

This function emits the colorChanged() signal if the new color is valid, and different from the old one.

References color(), colorChanged(), colorSet(), ColorPickerPopup::find(), insertColor(), ColorPickerPopup::setLastSel(), ColorPickerItem::setSelected(), and ColorPickerItem::text().

Referenced by SpreadsheetGui::Workbench::activated(), SpreadsheetGui::PropertiesDialog::PropertiesDialog(), and QtColorPicker().

◆ setStandardColors()

void QtColorPicker::setStandardColors ( )

Adds the 17 predefined colors from the Qt namespace.

(The names given to the colors, "Black", "White", "Red", etc., are all translatable.)

See also

References insertColor().

Referenced by SpreadsheetGui::Workbench::activated(), and SpreadsheetGui::PropertiesDialog::PropertiesDialog().

Property Documentation

◆ colorDialog


Whether the ellipsis "..." (more) button is available.

If this property is set to true, the color grid popup will include a "More" button (signified by an ellipsis, "...") which pops up a QColorDialog when clicked. The user will then be able to select any custom color they like.

The documentation for this class was generated from the following files: