ChatGPT解决这个技术问题 Extra ChatGPT

QLabel:设置文本和背景的颜色

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


J
Joundill

最好和推荐的方法是使用 Qt 样式表。文档:Qt 5 Style SheetQt 6 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: ; 作为“重置”似乎可以做到这一点,但这是一种好的做法,还是有更好的方法?
L
Linville

您可以使用 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 是这里的一个关键问题。如果没有该设置,上面接受的答案将不起作用。
C
Community

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

我进入了为我的绘画应用程序中的颜色显示标签设置 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() 不会使其工作。

问候,


p
paie

唯一对我有用的是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 和样式表都不适合我,非常烦人!
我更喜欢这种方式,因为它还允许您在 <font/> 标记中放置一些其他花哨的东西(HTML 人更熟悉的东西 :D),而不仅仅是一种颜色,因此它为您提供了更大的灵活性。
@iknownthing 样式表通过 QPalette 工作......一切都使用 QPalette。
L
Linville

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

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


在设计器中,单击“表单->查看代码”以查看生成的代码。
z
zubergu

这个工作完美

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->label->setStyleSheet( QString( "QLabel { background-color :%1; color : blue; }""+colcode+" ; color : blue; }" ).arg( color.name() ); // color.name 返回一个 #RRGGBB 格式的字符串

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅