How to Set a QLabel color using QColor

Posted on Posted in Qt Tutorials, Tutorials

Strange as it is, sometimes you need to go through some not-at-all-obvious procedures to solve simple problems such as this. QLabels allow writing and displaying HTML codes so one way of setting a QLabel’s color would be by setting the color as it is done in HTML but in Qt there is another way.

First, get the color selected using QColorDialog, and then set the style sheet of the label as it is shown below:

QColor clr = QColorDialog::getColor(Qt::white, this);
ui->label->setStyleSheet(“color: #” + QString::number(clr.rgb(), 16));

Leave a Reply

Your email address will not be published. Required fields are marked *