QColorDialog Class Reference

#include <qcolordialog.h>

Inheritance diagram for QColorDialog:

Inheritance graph
[legend]
Collaboration diagram for QColorDialog:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QColorDialog class provides a dialog widget for specifying colors.

The color dialog's function is to allow users to choose colors. For example, you might use this in a drawing program to allow the user to set the brush color.

The static functions provide modal color dialogs. If you require a modeless dialog, use the QColorDialog constructor.

The static getColor() function shows the dialog, and allows the user to specify a color. The getRgba() function does the same, but also allows the user to specify a color with an alpha channel (transparency) value.

The user can store customCount() different custom colors. The custom colors are shared by all color dialogs, and remembered during the execution of the program. Use setCustomColor() to set the custom colors, and use customColor() to get them.

Additional widgets that allow users to pick colors are available as {Qt Solutions}.

The {dialogs/standarddialogs}{Standard Dialogs} example shows how to use QColorDialog as well as other built-in Qt dialogs.

A color dialog in the Plastique widget style.

See also:
QColor, QFileDialog, QPrintDialog, QFontDialog, {Standard Dialogs Example}

Definition at line 37 of file qcolordialog.h.

Static Public Member Functions

static QColor getColor (const QColor &init=Qt::white, QWidget *parent=0)
static QRgb getRgba (QRgb, bool *ok=0, QWidget *parent=0)
static int customCount ()
static QRgb customColor (int)
static void setCustomColor (int, QRgb)
static void setStandardColor (int, QRgb)

Private Member Functions

 ~QColorDialog ()
 QColorDialog (QWidget *parent=0, bool modal=false)
void setColor (const QColor &)
QColor color () const
bool selectColor (const QColor &)
void setSelectedAlpha (int)
int selectedAlpha () const
void showCustom (bool=true)
 Q_PRIVATE_SLOT (d_func(), void _q_addCustom()) Q_PRIVATE_SLOT(d_func()
void _q_newHsv (int h, int s, int v)) Q_PRIVATE_SLOT(d_func()
void void _q_newColorTypedIn (QRgb rgb)) Q_PRIVATE_SLOT(d_func()
void void void _q_newCustom (int, int)) Q_PRIVATE_SLOT(d_func()
void void void void _q_newStandard (int, int)) friend class QColorShower


Constructor & Destructor Documentation

QColorDialog::~QColorDialog (  )  [private]

Destroys the color dialog.

Definition at line 1548 of file qcolordialog.cpp.

References QSettings::beginGroup(), cusrgb, customSet, QSettings::endGroup(), i, QString::number(), QSettings::setValue(), and QSettings::UserScope.

01549 {
01550 #ifndef QT_NO_SETTINGS
01551     if (!customSet) {
01552         QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
01553         settings.beginGroup(QLatin1String("Qt"));
01554         for (int i = 0; i < 2*8; ++i)
01555             settings.setValue(QLatin1String("customColors/") + QString::number(i), cusrgb[i]);
01556         settings.endGroup();
01557     }
01558 #endif
01559 }

Here is the call graph for this function:

QColorDialog::QColorDialog ( QWidget parent = 0,
bool  modal = false 
) [explicit, private]

Constructs a default color dialog called name with the given parent. If modal is true the dialog will be modal. Use setColor() to set an initial value.

See also:
getColor()

Definition at line 1437 of file qcolordialog.cpp.

References QSettings::beginGroup(), cusrgb, customSet, d, QSettings::endGroup(), i, QString::number(), QRgb, rgb, QDialog::setModal(), QDialog::setSizeGripEnabled(), QSettings::UserScope, and QSettings::value().

Referenced by getColor(), and getRgba().

01437                                                       :
01438     QDialog(*new QColorDialogPrivate, parent, (Qt::Dialog | Qt::WindowTitleHint |
01439                      Qt::MSWindowsFixedSizeDialogHint | Qt::WindowSystemMenuHint))
01440 {
01441     Q_D(QColorDialog);
01442     setModal(modal);
01443     setSizeGripEnabled(false);
01444     d->init();
01445 
01446 #ifndef QT_NO_SETTINGS
01447     if (!customSet) {
01448         QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
01449         settings.beginGroup(QLatin1String("Qt"));
01450         for (int i = 0; i < 2*8; ++i) {
01451             QVariant v = settings.value(QLatin1String("customColors/") + QString::number(i));
01452             if (v.isValid()) {
01453                 QRgb rgb = v.toUInt();
01454                 cusrgb[i] = rgb;
01455             }
01456         }
01457         settings.endGroup(); // Qt
01458     }
01459 #endif
01460 }

Here is the call graph for this function:


Member Function Documentation

QColor QColorDialog::getColor ( const QColor initial = Qt::white,
QWidget parent = 0 
) [static]

Pops up a modal color dialog, lets the user choose a color, and returns that color. The color is initially set to initial. The dialog is a child of parent. It returns an invalid (see QColor::isValid()) color if the user cancels the dialog. All colors allocated by the dialog will be deallocated before this function returns.

Definition at line 1471 of file qcolordialog.cpp.

References QDialog::Accepted, color(), QDialog::exec(), QObject::parent(), QColorDialog(), QDialog::result(), selectColor(), setColor(), and QWidget::setWindowTitle().

Referenced by MainWindow::brushColor(), ColorButton::changeColor(), qdesigner_internal::StyledButton::onEditor(), MainWindow::penColor(), SpreadSheet::selectColor(), Dialog::setColor(), and TextEdit::textColor().

01472 {
01473 #if defined(Q_WS_MAC)
01474     return macGetColor(initial, parent);
01475 #endif
01476 
01477     QColorDialog *dlg = new QColorDialog(parent, true);  //modal
01478     dlg->setWindowTitle(QColorDialog::tr("Select color"));
01479     dlg->setColor(initial);
01480     dlg->selectColor(initial);
01481     int resultCode = dlg->exec();
01482     QColor result;
01483     if (resultCode == QDialog::Accepted)
01484         result = dlg->color();
01485     delete dlg;
01486     return result;
01487 }

Here is the call graph for this function:

QRgb QColorDialog::getRgba ( QRgb  initial,
bool *  ok = 0,
QWidget parent = 0 
) [static]

Pops up a modal color dialog to allow the user to choose a color and an alpha channel (transparency) value. The color+alpha is initially set to initial. The dialog is a child of parent.

If ok is non-null, *ok is set to true if the user clicked OK, and to false if the user clicked Cancel.

If the user clicks Cancel, the initial value is returned.

Definition at line 1501 of file qcolordialog.cpp.

References QDialog::Accepted, alpha, c, color(), QDialog::exec(), QObject::parent(), qAlpha(), qBlue(), QColorDialog(), qGreen(), qRed(), QRgb, qRgba(), QDialog::result(), QColor::rgb(), selectColor(), selectedAlpha(), setColor(), setSelectedAlpha(), and QWidget::setWindowTitle().

Referenced by qdesigner_internal::QtColorButtonPrivate::slotEditColor().

01502 {
01503 #if defined(Q_WS_MAC)
01504     return macGetRgba(initial, ok, parent);
01505 #endif
01506 
01507     QColorDialog *dlg = new QColorDialog(parent, true);  //modal
01508 
01509     dlg->setWindowTitle(QColorDialog::tr("Select color"));
01510     dlg->setColor(initial);
01511     dlg->selectColor(initial);
01512     dlg->setSelectedAlpha(qAlpha(initial));
01513     int resultCode = dlg->exec();
01514     QRgb result = initial;
01515     if (resultCode == QDialog::Accepted) {
01516         QRgb c = dlg->color().rgb();
01517         int alpha = dlg->selectedAlpha();
01518         result = qRgba(qRed(c), qGreen(c), qBlue(c), alpha);
01519     }
01520     if (ok)
01521         *ok = resultCode == QDialog::Accepted;
01522 
01523     delete dlg;
01524     return result;
01525 }

Here is the call graph for this function:

int QColorDialog::customCount (  )  [static]

Returns the number of custom colors supported by QColorDialog. All color dialogs share the same custom colors.

Definition at line 453 of file qcolordialog.cpp.

Referenced by customColor(), and setCustomColor().

00454 {
00455     return 2*8;
00456 }

QRgb QColorDialog::customColor ( int  i  )  [static]

Returns custom color number i as a QRgb value.

Definition at line 461 of file qcolordialog.cpp.

References cusrgb, customCount(), and initRGB().

00462 {
00463     initRGB();
00464     Q_ASSERT(i >= 0 && i < customCount());
00465     return cusrgb[i];
00466 }

Here is the call graph for this function:

void QColorDialog::setCustomColor ( int  number,
QRgb  color 
) [static]

Sets the custom color number to the QRgb color value.

Definition at line 473 of file qcolordialog.cpp.

References cusrgb, customCount(), customSet, and initRGB().

00474 {
00475     initRGB();
00476     Q_ASSERT(i >= 0 && i < customCount());
00477     customSet = true;
00478     cusrgb[i] = c;
00479 }

Here is the call graph for this function:

void QColorDialog::setStandardColor ( int  number,
QRgb  color 
) [static]

Sets the standard color number to the QRgb color value given.

Definition at line 487 of file qcolordialog.cpp.

References initRGB(), and stdrgb.

00488 {
00489     initRGB();
00490     Q_ASSERT(i >= 0 && i < 6*8);
00491     stdrgb[i] = c;
00492 }

Here is the call graph for this function:

void QColorDialog::setColor ( const QColor color  )  [private]

Sets the color shown in the dialog to the color given.

See also:
color()

Definition at line 1570 of file qcolordialog.cpp.

References c, and d.

Referenced by getColor(), and getRgba().

01571 {
01572     Q_D(QColorDialog);
01573     d->setCurrentColor(c.rgb());
01574 }

QColor QColorDialog::color (  )  const [private]

Returns the currently selected color in the dialog.

See also:
setColor()

Definition at line 1537 of file qcolordialog.cpp.

References d.

Referenced by getColor(), getRgba(), and selectColor().

01538 {
01539     Q_D(const QColorDialog);
01540     return QColor(d->currentColor());
01541 }

bool QColorDialog::selectColor ( const QColor col  )  [private]

Sets focus to the corresponding button, if any.

Definition at line 1607 of file qcolordialog.cpp.

References color(), cusrgb, d, i, j, QRgb, QColor::rgb(), and stdrgb.

Referenced by getColor(), and getRgba().

01608 {
01609     Q_D(QColorDialog);
01610     QRgb color = col.rgb();
01611     int i = 0, j = 0;
01612     // Check standard colors
01613     if (d->standard) {
01614         for (i = 0; i < 6; i++) {
01615             for (j = 0; j < 8; j++) {
01616                 if (color == stdrgb[i + j*6]) {
01617                     d->_q_newStandard(i, j);
01618                     d->standard->setCurrent(i, j);
01619                     d->standard->setSelected(i, j);
01620                     d->standard->setFocus();
01621                     return true;
01622                 }
01623             }
01624         }
01625     }
01626     // Check custom colors
01627     if (d->custom) {
01628         for (i = 0; i < 2; i++) {
01629             for (j = 0; j < 8; j++) {
01630                 if (color == cusrgb[i + j*2]) {
01631                     d->_q_newCustom(i, j);
01632                     d->custom->setCurrent(i, j);
01633                     d->custom->setSelected(i, j);
01634                     d->custom->setFocus();
01635                     return true;
01636                 }
01637             }
01638         }
01639     }
01640     return false;
01641 }

Here is the call graph for this function:

void QColorDialog::setSelectedAlpha ( int  alpha  )  [private]

Sets the initial alpha channel value to the alpha value given, and shows the alpha channel entry box.

Definition at line 1586 of file qcolordialog.cpp.

References d.

Referenced by getRgba().

01587 {
01588     Q_D(QColorDialog);
01589     d->showAlpha(true);
01590     d->setCurrentAlpha(a);
01591 }

int QColorDialog::selectedAlpha (  )  const [private]

Returns the value selected for the alpha channel.

Definition at line 1598 of file qcolordialog.cpp.

References d.

Referenced by getRgba().

01599 {
01600     Q_D(const QColorDialog);
01601     return d->currentAlpha();
01602 }

void QColorDialog::showCustom ( bool  = true  )  [private]

QColorDialog::Q_PRIVATE_SLOT ( d_func()  ,
void   _q_addCustom() 
) [private]

Reimplemented from QWidget.

void QColorDialog::_q_newHsv ( int  h,
int  s,
int  v 
) [private]

void void QColorDialog::_q_newColorTypedIn ( QRgb  rgb  )  [private]

void void void QColorDialog::_q_newCustom ( int  ,
int   
) [private]

void void void void QColorDialog::_q_newStandard ( int  ,
int   
) [private]


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 17:01:00 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1