Ralf Wickum
Ralf Wickum

Reputation: 3270

How to display a QColor from QColorDialog in a widget?

I have a ColorPicker dialog like:

QColor color = QColorDialog::getColor(Qt::black, this, "Pick a color",  QColorDialog::DontUseNativeDialog);

The result of that I put in a QLineEdit via color.name() , e.g. #ff0000 . I would like to display that color as the red field in this example, too

enter image description here

I don't know what Widget to pick for this to display? QPicture?

Upvotes: 2

Views: 3324

Answers (2)

Gaurang
Gaurang

Reputation: 87

One way to do it, if you have QColor in color and QLabel *label -

label->setStyleSheet("background-color:"+color.name()+";");

Upvotes: 0

Murat
Murat

Reputation: 736

I enhanced this answer here. If you already grabbed the QColor in color, you can try for a QLabel* label:

QPalette palette = label->palette();
palette.setColor(label->backgroundRole(), color);    
label->setAutoFillBackground(true);
label->setPalette(palette);

Upvotes: 2

Related Questions