【问题标题】:QLabel change font color without changing any other styleQLabel 更改字体颜色而不更改任何其他样式
【发布时间】:2019-08-24 16:51:44
【问题描述】:

我想动态更改 QLabel 中文本的颜色。 我已经在 ui 文件中定义了 QLabel 的颜色和样式,我想在某个事件发生时更改它。 我想改变颜色而不改变我的 QLabel 的任何其他样式。

我找到了几个解决 QLabel 中文本颜色更改问题的答案(123),它们都使用函数 setStyleSheet。这个函数工作正常,但它改变了我的字体大小和其他与 QLabel 相关的样式。

我发现问题与 setStyleSheet ignoring any previous style 有关。那里提出的解决方案涉及检索我想要维护的所有样式并再次设置它们以及文本颜色更改。

这很麻烦且难以维护。如果将来定义了更多样式,我需要查看这部分代码才能重置所有样式。

我希望能够在不更改任何其他样式的情况下更改 QLabel 文本颜色。有可能吗?

【问题讨论】:

  • 为什么不使用setPalette函数?
  • @Simon one of the answers I posted above 不鼓励使用 setPalette。你不同意这个答案吗?
  • 好吧,我刚刚在文档中读到: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. 也许它是最好的解决方案...
  • @Simon 现在,我不需要更复杂的自定义,setPalette 工作正常。我担心我发布的答案中提到的可移植性问题。

标签: qt qtstylesheets qlabel


【解决方案1】:

您可以创建一些样式类来控制小部件的样式:

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;
}

代码中的某处:

WidgetStyleSheet labelSS;
// ...

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


label->setStyleSheet(labelSS);

【讨论】:

    【解决方案2】:

    务实的做法:

    利用 CSS 的级联性。

    • 将您的QLabel 包裹在QWidget 中(不要忘记QLayout)。
    • QWidget周围设置默认样式。
    • 将字体颜色设置为QLabel的唯一样式。

    【讨论】:

      【解决方案3】:

      如果你想管理QLabel的文字颜色,你可以用自定义的类来包装它。

      例如:

      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);
          }
      };
      

      并在您的代码中使用它:

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

      仅供参考:我在 Fedora、Qt5.12 上对其进行了测试,它运行良好。

      【讨论】:

        【解决方案4】:

        以下工作正常。但它并不那么优雅。这是在python中。您必须将按钮名称(或任何其他名称)传递给数组中定义的以下内容

                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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-23
          • 2014-08-24
          • 2014-12-24
          • 2012-02-25
          • 2015-03-19
          • 2020-12-01
          • 2018-10-29
          • 1970-01-01
          相关资源
          最近更新 更多