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

时间:2023-02-04 19:35:27

How do I set color of text and background of a QLabel ?

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

6 个解决方案

#1


199  

The best and recommended way is to use Qt Style Sheet.

最好的推荐方法是使用Qt样式表。

To change the text color and background color of a QLabel, here is what I would do :

为了改变QLabel的文本颜色和背景颜色,下面是我要做的:

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

You could also avoid using Qt Style Sheets and change the QPalette colors of your QLabel, but you might get different results on different platforms and/or styles.

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

As Qt documentation states :

如Qt文件所述:

Using a QPalette isn't guaranteed to work for all styles, because style authors are restricted by the different platforms' guidelines and by the native theme engine.

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

But you could 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);

But as I said, I strongly suggest not to use the palette and go for Qt Style Sheet.

但是正如我说的,我强烈建议不要使用调色板,选择Qt样式表。

#2


27  

You can use QPalette, however you must set setAutoFillBackground(true); to enable background color

您可以使用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");

It works fine on Windows and Ubuntu, I haven't played with any other OS.

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

Note: Please see QPalette, color role section for more details

注意:请参见QPalette, color role部分,了解更多细节。

#3


13  

I add this answer because I think it could be useful to anybody.

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

I step into the problem of setting RGBA colors (that is, RGB color with an Alpha value for transparency) for color display labels in my painting application.

在我的绘画应用程序中,我进入了设置RGBA颜色(即RGB颜色和透明度的Alpha值)的问题。

As I came across the first answer, I was unable to set an RGBA color. I have also tried things like:

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

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

myLabel。set样式表(“QLabel {backbackground -color: %s”%color.name())

where color is an RGBA color.

颜色是RGBA颜色。

So, my dirty solution was to extend QLabel and override paintEvent() method filling its bounding rect.

因此,我的脏解决方案是扩展QLabel并覆盖paintEvent()方法填充它的边界rect。

Today, I've open up the qt-assistant and read the style reference properties list. Affortunately, it has an example that states the following:

今天,我打开了qt-assistant并阅读了样式引用属性列表。幸运的是,它有这样一个例子:

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

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

Thats open up my mind in doing something like the code below, as an 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+"); }")

Note that setAutoFillBackground() set in False will not make it work.

注意,setAutoFillBackground()设置为False不会使其工作。

Regards,

问候,

#4


11  

The best way to set any feature regarding the colors of any widget is to use QPalette.

设置任何小部件的颜色特征的最佳方法是使用QPalette。

And the easiest way to find what you are looking for is to open Qt Designer and set the palette of a QLabel and check the generated code.

最简单的方法就是打开Qt设计器并设置QLabel的调色板,并检查生成的代码。

#5


10  

The ONLY thing that worked for me was html.

对我来说唯一有用的是html。

And I found it to be the far easier to do than any of the programmatic approaches.

我发现它比任何一种编程方法都要容易得多。

The following code changes the text color based on a parameter passed by a caller.

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

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

#6


2  

This one is working perfect

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() method returns the selected color. You can change label color using stylesheet

getColor()方法返回所选的颜色。您可以使用样式表更改标签颜色。

#1


199  

The best and recommended way is to use Qt Style Sheet.

最好的推荐方法是使用Qt样式表。

To change the text color and background color of a QLabel, here is what I would do :

为了改变QLabel的文本颜色和背景颜色,下面是我要做的:

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

You could also avoid using Qt Style Sheets and change the QPalette colors of your QLabel, but you might get different results on different platforms and/or styles.

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

As Qt documentation states :

如Qt文件所述:

Using a QPalette isn't guaranteed to work for all styles, because style authors are restricted by the different platforms' guidelines and by the native theme engine.

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

But you could 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);

But as I said, I strongly suggest not to use the palette and go for Qt Style Sheet.

但是正如我说的,我强烈建议不要使用调色板,选择Qt样式表。

#2


27  

You can use QPalette, however you must set setAutoFillBackground(true); to enable background color

您可以使用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");

It works fine on Windows and Ubuntu, I haven't played with any other OS.

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

Note: Please see QPalette, color role section for more details

注意:请参见QPalette, color role部分,了解更多细节。

#3


13  

I add this answer because I think it could be useful to anybody.

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

I step into the problem of setting RGBA colors (that is, RGB color with an Alpha value for transparency) for color display labels in my painting application.

在我的绘画应用程序中,我进入了设置RGBA颜色(即RGB颜色和透明度的Alpha值)的问题。

As I came across the first answer, I was unable to set an RGBA color. I have also tried things like:

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

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

myLabel。set样式表(“QLabel {backbackground -color: %s”%color.name())

where color is an RGBA color.

颜色是RGBA颜色。

So, my dirty solution was to extend QLabel and override paintEvent() method filling its bounding rect.

因此,我的脏解决方案是扩展QLabel并覆盖paintEvent()方法填充它的边界rect。

Today, I've open up the qt-assistant and read the style reference properties list. Affortunately, it has an example that states the following:

今天,我打开了qt-assistant并阅读了样式引用属性列表。幸运的是,它有这样一个例子:

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

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

Thats open up my mind in doing something like the code below, as an 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+"); }")

Note that setAutoFillBackground() set in False will not make it work.

注意,setAutoFillBackground()设置为False不会使其工作。

Regards,

问候,

#4


11  

The best way to set any feature regarding the colors of any widget is to use QPalette.

设置任何小部件的颜色特征的最佳方法是使用QPalette。

And the easiest way to find what you are looking for is to open Qt Designer and set the palette of a QLabel and check the generated code.

最简单的方法就是打开Qt设计器并设置QLabel的调色板,并检查生成的代码。

#5


10  

The ONLY thing that worked for me was html.

对我来说唯一有用的是html。

And I found it to be the far easier to do than any of the programmatic approaches.

我发现它比任何一种编程方法都要容易得多。

The following code changes the text color based on a parameter passed by a caller.

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

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

#6


2  

This one is working perfect

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() method returns the selected color. You can change label color using stylesheet

getColor()方法返回所选的颜色。您可以使用样式表更改标签颜色。