views:

1393

answers:

3

What is the best way to give the user feedback for a color selection?
I have a dialog with a "Select Color" push button which pops a QColorDialog. After the selection is made I want to show the user the color selected.
Usually I do this using a QLabel and changing it's background color in the palette. This method is cumbersome and I think not very portable.
Is there a standard way of doing this?

+3  A: 

The way I'm doing it is the following :

I actually change the color of the button, to reflect the user choice. To do this, I'm using the Qt style sheet, which ensure it is portable :

const QString COLOR_STYLE("QPushButton { background-color : %1; color : %2; }");

QColor ChosenColor; // Color chosen by the user with QColorDialog
QColor IdealTextColor = getIdealTextColor(ChosenColor);
btnChooseColor->setStyleSheet(COLOR_STYLE.arg(ChosenColor.name()).arg(IdealTextColor.name()));

To make sure the label of the button is always readable, I'm calling the method getIdealTextColor(), which is a method I've found from a codeproject article :

//==============================================================================
//  Nom : getIdealTextColor
//! @return an ideal label color, based on the given background color.
//! Based on http://www.codeproject.com/cs/media/IdealTextColor.asp
//==============================================================================
QColor JSPreferencesDlg::getIdealTextColor(const QColor& rBackgroundColor) const
{
    const int THRESHOLD = 105;
    int BackgroundDelta = (rBackgroundColor.red() * 0.299) + (rBackgroundColor.green() * 0.587) + (rBackgroundColor.blue() * 0.114);
    return QColor((255- BackgroundDelta < THRESHOLD) ? Qt::black : Qt::white);
}
Jérôme
A: 

The method you are using is cumbersome, but what makes you think it isn't portable? It should work anywhere Qt does.

Besides that, I would try to make a color-filled button somehow, similar to Jérôme's answer, only I would probably make a dedicated button to do it, and mess with the palette somehow.

Caleb Huitt - cjhuitt
+1  A: 
David Dibben