QLabel: set color of text and background
QLabel: set color of text and background
Change the text color and background color of a QLabel:
QLabel* pLabel = new QLabel; pLabel->setStyleSheet("QLabel { background-color : red; color : blue; }");
using Qt Style Sheets and change the QPalette colors of your QLabel, get different results on different platforms and styles.
Qt documentation states :
Using a QPalette is not guaranteed to work for all styles, because style authors are restricted by the different platforms’ guidelines and by the native theme engine.
Do something like this :
QPalette palette = ui->pLabel->palette(); palette.setColor(ui->pLabel->backgroundRole(), Qt::yellow); palette.setColor(ui->pLabel->foregroundRole(), Qt::yellow); ui->pLabel->setPalette(palette);
Use this:
QPalette,
must set setAutoFillBackground(true); to enable background color
QPalette sample_palette; sample_palette.setColor(QPalette::Window, Qt::white); sample_palette.setColor(QPalette::WindowText, Qt::blue); sample_label->setAutoFillBackground(true); sample_label->setPalette(sample_palette); sample_label->setText("What ever text");
It works on Windows and Ubuntu, have not played with any other OS.
For more information just try this link
QPalette, color role section for more details
Easy method:
The setting RGBA colors (that is, RGB color with an Alpha value for transparency) for color display labels in my painting application.
myLabel.setStyleSheet(“QLabel { background-color : %s”%color.name())
where color is an RGBA color.
So, my dirty solution was to extend QLabel and override paintEvent() method filling its bounding rect.
Today, I have
open up the qt-assistant and read the style reference properties list. Example to states the following:
QLineEdit { background-color: rgb(255, 0, 0) }
The given code is example:
myLabel= QLabel() myLabel.setAutoFillBackground(True) # This is important!! color = QtGui.QColor(233, 10, 150) alpha = 140 values = "{r}, {g}, {b}, {a}".format(r = color.red(), g = color.green(), b = color.blue(), a = alpha myLabel.setStyleSheet("QLabel { background-color: rgba("+values+"); }")
setAutoFillBackground() set is worked.