【问题标题】:QLabel: set color of text and backgroundQLabel:设置文本和背景的颜色
【发布时间】:2011-02-14 12:59:38
【问题描述】:

如何设置QLabel 的文字和背景颜色?

【问题讨论】:

    标签: qt qt4 qlabel


    【解决方案1】:

    最好和推荐的方法是使用Qt Style Sheet

    要更改QLabel 的文本颜色和背景颜色,我会这样做:

    QLabel* pLabel = new QLabel;
    pLabel->setStyleSheet("QLabel { background-color : red; color : blue; }");
    

    您也可以避免使用 Qt 样式表并更改您的 QLabelQPalette 颜色,但您可能会在不同的平台和/或样式上得到不同的结果。

    正如 Qt 文档所述:

    使用 QPalette 并不能保证适用于所有样式,因为样式作者受到不同平台指南和本机主题引擎的限制。

    但你可以这样做:

     QPalette palette = ui->pLabel->palette();
     palette.setColor(ui->pLabel->backgroundRole(), Qt::yellow);
     palette.setColor(ui->pLabel->foregroundRole(), Qt::yellow);
     ui->pLabel->setPalette(palette);
    

    但正如我所说,我强烈建议不要使用调色板并使用 Qt 样式表。

    【讨论】:

    • 我一直在使用 setStyleSheet() 方法,至少在 Qt 4.4 中,它最终会在样式表中调用 connect 并导致内存使用量增加。
    • 我打开了一个关于内存使用增加的错误报告,可以在here找到。
    • color 属性无效。只有通过 HTML <font color="#FFFFFF">...</font> 我才能设置字体颜色(在这种情况下为白色。
    • 有没有办法指定用户桌面的默认(文本)颜色?使用color: ; 作为“重置”似乎可以做到这一点,但这是一种好的做法,还是有更好的方法?
    【解决方案2】:

    您可以使用 QPalette,但您必须设置 setAutoFillBackground(true); 才能启用背景颜色

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

    它在 Windows 和 Ubuntu 上运行良好,我没有玩过任何其他操作系统。

    注意:请参阅QPalette,颜色角色部分了解更多详情

    【讨论】:

    • 这是任何方法中最重要的单个元素(样式表除外)。
    • 感谢您指出 autoFillBackground 是这里的关键问题。如果没有该设置,上面接受的答案将不起作用。
    【解决方案3】:

    我添加这个答案是因为我认为它对任何人都有用。

    我遇到了在我的绘画应用程序中为颜色显示标签设置RGBA 颜色(即透明度为 Alpha 值的 RGB 颜色)的问题。

    当我遇到第一个答案时,我无法设置 RGBA 颜色。我也尝试过:

    myLabel.setStyleSheet("QLabel { background-color : %s"%color.name())

    color 是一种 RGBA 颜色。

    所以,我的肮脏解决方案是扩展 QLabel 并覆盖 paintEvent() 填充其边界矩形的方法。

    今天,我打开了qt-assistant 并阅读了style reference properties list。幸运的是,它有一个例子说明如下:

    QLineEdit { background-color: rgb(255, 0, 0) }

    这让我大开脑洞,比如下面的代码:

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

    请注意,在False 中设置的setAutoFillBackground() 不会使其工作。

    问候,

    【讨论】:

      【解决方案4】:

      唯一对我有用的是 html。

      而且我发现它比任何编程方法都容易得多。

      以下代码根据调用者传递的参数更改文本颜色。

      enum {msg_info, msg_notify, msg_alert};
      :
      :
      void bits::sendMessage(QString& line, int level)
      {
          QTextCursor cursor = ui->messages->textCursor();
          QString alertHtml  = "<font color=\"DeepPink\">";
          QString notifyHtml = "<font color=\"Lime\">";
          QString infoHtml   = "<font color=\"Aqua\">";
          QString endHtml    = "</font><br>";
      
          switch(level)
          {
              case msg_alert:  line = alertHtml % line; break;
              case msg_notify: line = notifyHtml % line; break;
              case msg_info:   line = infoHtml % line; break;
              default:         line = infoHtml % line; break;
          }
      
          line = line % endHtml;
          ui->messages->insertHtml(line);
          cursor.movePosition(QTextCursor::End);
          ui->messages->setTextCursor(cursor);
      }
      

      【讨论】:

      • 这里也一样,QPalette 和样式表都不适合我,很烦人!
      • 我更喜欢这种方式,因为它还允许您在 &lt;font/&gt; 标记中放入一些其他花哨的东西(HTML 人更熟悉的东西:D),而不仅仅是一种颜色,因此它给了您更大的灵活性。
      • @iknownothing 样式表通过 QPalette 工作...一切都使用 QPalette。
      【解决方案5】:

      设置有关任何小部件颜色的任何功能的最佳方法是使用QPalette

      找到您要查找的内容的最简单方法是打开 Qt Designer 并设置 QLabel 的调色板并检查生成的代码。

      【讨论】:

      • 在设计器中,点击“Form->View Code”查看生成的代码。
      【解决方案6】:

      这个工作很完美

      QColorDialog *dialog = new QColorDialog(this);
      QColor color=  dialog->getColor();
      QVariant variant= color;
      QString colcode = variant.toString();
      ui->label->setStyleSheet("QLabel { background-color :"+colcode+" ; color : blue; }");
      

      getColor() 方法返回选择的颜色。 您可以使用stylesheet更改标签颜色

      【讨论】:

      • 虽然代码很受欢迎,但它应该总是有一个附带的解释。这不必很长,但在意料之中。
      • 虽然这段代码有效,但有一些明确的优化 QColor color = QColorDialog::getColor( QColor( Qt::white ), this, tr( "Select Color" ); // 使用选择颜色的静态函数,初始值为白色 ui-&gt;label-&gt;setStyleSheet( QString( "QLabel { background-color :%1; color : blue; }""+colcode+" ; color : blue; }" ).arg( color.name() ); // color.name 返回一个 #RRGGBB 格式的字符串
      猜你喜欢
      • 2013-07-29
      • 2022-01-22
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多