|  | Home

# QtColorPicker Class Reference

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

     #include <QtColorPicker>

Inherits QPushButton.

- List of all members, including inherited members

### Properties

- colorDialog : bool

- 3 properties inherited from QPushButton
- 11 properties inherited from QAbstractButton
- 57 properties inherited from QWidget
- 1 property inherited from QObject

### Public Functions

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

- 8 public functions inherited from QPushButton
- 21 public functions inherited from QAbstractButton
- 207 public functions inherited from QWidget
- 12 public functions inherited from QPaintDevice
- 29 public functions inherited from QObject

### Public Slots

- void setCurrentColor ( const QColor & color )

- 1 public slot inherited from QPushButton
- 5 public slots inherited from QAbstractButton
- 19 public slots inherited from QWidget
- 1 public slot inherited from QObject

### Signals

- void colorChanged ( const QColor & color )

- 4 signals inherited from QAbstractButton
- 1 signal inherited from QWidget
- 1 signal inherited from QObject

### Static Public Members

- QColor getColor ( const QPoint & point, bool allowCustomColors = true )

- 4 static public members inherited from QWidget
- 4 static public members inherited from QObject

### Additional Inherited Members

- 1 public type inherited from QObject
- 1 protected function inherited from QPushButton
- 3 protected functions inherited from QAbstractButton
- 37 protected functions inherited from QWidget
- 1 protected function inherited from QPaintDevice
- 7 protected functions inherited from QObject
- 1 protected slot inherited from QWidget
- 1 protected type inherited from QPaintDevice
- 2 protected variables inherited from QObject

## 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.

See also QColorDialog.

## Property Documentation

### colorDialog : bool

This property holds 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.

Access functions:

- bool colorDialogEnabled () const
- void setColorDialogEnabled ( bool enabled )

## Member Function 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.

### QtColorPicker::~QtColorPicker ()

Destructs the QtColorPicker.

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

Returns the color at position index.

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

### QColor QtColorPicker::currentColor () const

Returns the currently selected color.

See also setCurrentColor() and text().

### 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()));
         }
     }

### 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.

### 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.

See also currentColor().

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