QtColorPicker Class Reference

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

`#include <qtcolorpicker.h>`

Inheritance diagram for QtColorPicker:

## Public Slots

void | setCurrentColor (const QColor &col)

## Signals

void | colorChanged (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::null, int index=-1)
| QtColorPicker (QWidget *parent=0, 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)

## Properties

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

## Private Slots

void | buttonPressed (bool toggled)
void | popupClosed ()

## Private Attributes

QColor | col
bool | dirty
bool | firstInserted
ColorPickerPopup * | popup
bool | withColorDialog

## 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"));
picker->insertColor(white);
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.

colorpicker1.png colorpicker2.png

See also
  QColorDialog

## Constructor & Destructor Documentation

QtColorPicker::QtColorPicker | ( | QWidget * | parent = `0`,
|  | 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
  QFrame

References buttonPressed(), col, dirty, firstInserted, popup, popupClosed(), setCurrentColor(), and withColorDialog.

QtColorPicker::~QtColorPicker | ( |  | ) |

Destructs the QtColorPicker.

## Member Function Documentation

void QtColorPicker::buttonPressed | ( | bool | toggled | ) |

private slot

References col, ColorPickerPopup::find(), and popup.

Referenced by QtColorPicker().

QColor QtColorPicker::color | ( | int | index | ) | const

Returns the color at position index.

References ColorPickerPopup::color(), and popup.

Referenced by insertColor(), paintEvent(), and setCurrentColor().

QtColorPicker::colorChanged | ( | const QColor & | color | ) |

signal

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().

bool QtColorPicker::colorDialogEnabled | ( |  | ) | const

References withColorDialog.

QColor QtColorPicker::currentColor | ( |  | ) | const

Returns the currently selected color.

See also
  text()

References col.

Referenced by ColorButton::color().

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

static

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(), ColorPickerPopup::lastSelected(), and popup.

void QtColorPicker::insertColor | ( | const QColor & | color,
|  | const QString & | text = `QString::null`,
|  | 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 col, color(), firstInserted, ColorPickerPopup::insertColor(), and popup.

Referenced by ColorButton::ColorButton(), setCurrentColor(), and setStandardColors().

void QtColorPicker::paintEvent | ( | QPaintEvent * | e | ) |

protected

References col, color(), and dirty.

void QtColorPicker::popupClosed | ( |  | ) |

private slot

Referenced by QtColorPicker().

void QtColorPicker::setColorDialogEnabled | ( | bool | enabled | ) |

References withColorDialog.

void QtColorPicker::setCurrentColor | ( | const QColor & | color | ) |

slot

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 col, color(), colorChanged(), dirty, ColorPickerPopup::find(), insertColor(), popup, ColorPickerItem::setSelected(), and ColorPickerItem::text().

Referenced by QtColorPicker(), and ColorButton::setColor().

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
  insertColor()

References insertColor().

## Member Data Documentation

QColor QtColorPicker::col

private

Referenced by buttonPressed(), currentColor(), insertColor(), paintEvent(), QtColorPicker(), and setCurrentColor().

bool QtColorPicker::dirty

private

Referenced by paintEvent(), QtColorPicker(), and setCurrentColor().

bool QtColorPicker::firstInserted

private

Referenced by insertColor(), and QtColorPicker().

ColorPickerPopup* QtColorPicker::popup

private

Referenced by buttonPressed(), color(), getColor(), insertColor(), QtColorPicker(), and setCurrentColor().

bool QtColorPicker::withColorDialog

private

Referenced by colorDialogEnabled(), QtColorPicker(), and setColorDialogEnabled().

## Property Documentation

QtColorPicker::colorDialog

read write

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:

- qtiplot/src/lib/3rdparty/qtcolorpicker/src/qtcolorpicker.h
- qtiplot/src/lib/3rdparty/qtcolorpicker/src/qtcolorpicker.cpp