0
votes

I want to change the color of the text in a QLabel dynamically. I have defined the color and style of the QLabel in the ui file and I want to change it when a certain event takes place. I want to change the color without changing any other style of my QLabel.

I have found several answers adressing the issue of changing text color in a QLabel (1, 2, 3) and they all use the function setStyleSheet. This function works fine but it changes my font size and other styles related to the QLabel.

I have seen that the problem is related to setStyleSheet ignoring any previous style. The solution proposed there involves retrieving all the styles I want to maintain and setting them again together with the text color change.

This is cumbersome and difficult to maintain. If more styles were defined in the future I would need to review this part of the code to be able to reset all of them.

I would like to be able to change QLabel text color without altering any other syle. Is it possible?

4
why not to use the setPalette function?Simon
@Simon The use of setPalette is discouraged in one of the answers I posted above. Do you disagree with this answer?fa__
well I just read that in the documentation: Style sheets let you perform all kinds of customizations that are difficult or impossible to perform using QPalette alone. If you want yellow backgrounds for mandatory fields, red text for potentially destructive push buttons, or fancy check boxes, style sheets are the answer. maybe its the best solution...Simon
@Simon For now, I do not need a more complex customization, setPalette works fine. I am worried about the portability issues mentioned in the answer I have posted.fa__

4 Answers

1
votes

If you want to manage the text color of QLabel you could wrap it with customized class.

For example:

class ColorLabel : public QLabel
{
public:
    ColorLabel(const QString &text, QWidget *parent = nullptr)
        : QLabel(text, parent)
    {
        setAutoFillBackground(true);
    }

    void setTextColor(const QColor &color)
    {
        QPalette palette = this->palette();
        palette.setColor(this->backgroundRole(), color);
        palette.setColor(this->foregroundRole(), color);
        this->setPalette(palette);
    }
};

And to use it in your code:

 ColorLabel * poColorLabel = new ColorLabel("My string", this);
 poColorLabel->setTextColor(Qt::red); // set label text in red color

FYI: I tested it on Fedora, Qt5.12 and it works fine.

0
votes

A pragmatic approach:

Utilize the cascadingness of CSS.

  • Wrap your QLabel in a QWidget (don't forget a QLayout).
  • Set your default style on the surrounding QWidget.
  • Set the font color as the QLabel's only style.
0
votes

You can create some style class to control a widget's style:

class WidgetStyleSheet
{
public:
    // change some style's value
    void setValue(const QString& styleKey, const QString& value)
    {
        _styleMap[styleKey] = value;
    }

    // to default state
    void reset() {}

    // form stylesheet
    QString toStyleSheet() const
    {
        QString styleSheet;
        QMapIterator<QString, QString> iter(_styleMap);
        while( iter.hasNext() )
            styleSheet += QString("%1: %2").arg(iter.key()).arg(iter.value());
        return styleSheet;
    }
private:
    QMap<QString, QString> _styleMap;
}

Somewhere in your code:

WidgetStyleSheet labelSS;
// ...

labelSS.setValue("color", QString("%1").arg( QColor(255, 10, 0).name() );
labelSS.setValue("background-color", "...");
// ...


label->setStyleSheet(labelSS);
0
votes

The following works fine. But it is not that elegant. This is in python. You have to pass the button name (or any other) to the following as is defined in the array

        btns = ['self.hBeamBtn','self.lBeamBtn','self.allTestBtn','self.prnStatusBtn']
        for btn in btns:
            if  str(btn_name) == str(btn):
                styl = btn+'.setStyleSheet("font: bold;background-color: red;font-size: 12px;height: 28px;width: 80px;")'
                eval(styl)