C++ 更改 QProgressBar 的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9368824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:44:04  来源:igfitidea点击:

Change the color of a QProgressBar

c++qtqprogressbar

提问by steps

I am running ubuntu 11.04. This is what my progress bars look like:

我正在运行 ubuntu 11.04。这是我的进度条的样子:

progress bar

进度条

I am showing the progress bars in a batch processing window (one per batch item) and would like to use them as a status indicator (green while all is going well, red in case of errors, ...).

我在批处理窗口中显示进度条(每个批处理项目一个),并希望将它们用作状态指示器(绿色表示一切顺利,红色表示错误,......)。

I have tried several suggestions, including the ones made to thisalmost identical question. Unfortunately, I couldn't make it work and the documentationon customizing QProgressBars doesn't help me either, so I would be very grateful for any suggestions as to what I'm doing wrong.

我尝试了几个建议,包括针对这个几乎相同的问题提出的建议。不幸的是,我无法让它工作,关于自定义 QProgressBars的文档也没有帮助我,所以我将非常感谢任何关于我做错了什么的建议。

I have subclassed the QProgressBar as suggested and have tried using stylesheets as well as the palette (not at the same time but as alternatives). With stylesheets, I can't make it look anything like the regular progress bars. Changing the color is really all I want to do, so I figured it should be much easier to do that by use of a palette instead of a stylesheet, but with the palette nothing happens at all.

我已经按照建议对 QProgressBar 进行了子类化,并尝试使用样式表和调色板(不是同时使用,而是作为替代方案)。使用样式表,我不能让它看起来像常规的进度条。改变颜色确实是我想要做的,所以我认为通过使用调色板而不是样式表应该更容易做到这一点,但使用调色板根本没有任何反应。

Here is one of the versions I've tried for the palette:

这是我为调色板尝试过的版本之一:

#include "myprogressbar.h"

#include <QtGui/QPalette>

MyProgressBar::MyProgressBar(QWidget *parent) :
    QProgressBar(parent)
{}

void MyProgressBar::onProgress(int value, int maximum, QString phase)
{
    setMaximum(maximum);
    setValue(value);
    setFormat(phase);

    QPalette p = this->palette();
    p.setColor(QPalette::Highlight, QColor(Qt::green));
    this->setPalette(p);
}

...

I also tried the version suggested here, but that didn't help either.

我也尝试了此处建议的版本,但这也无济于事。

回答by castors33

It tried this :

它试过这个:

QProgressBar {
     border: 2px solid grey;
     border-radius: 5px;
     background-color: #FF0000;
 }

 QProgressBar::chunk {
     background-color: #05B8CC;
     width: 20px;
 }

as styleSheet for the progressBar and I got this enter image description here

作为进度条的样式表,我得到了这个 在此处输入图片说明

so it is easy to change the background of the bar to the color you want and you can display a text by yourself with setFormat(). Is it working for you?

因此很容易将栏的背景更改为您想要的颜色,并且您可以使用setFormat(). 它对你有用吗?

回答by Arwen

I had this problem too, but I find a way, with the help of this site: http://thesmithfam.org/blog/2009/10/13/cool-qprogressbar-stylesheet/

我也有这个问题,但我找到了一个方法,在这个网站的帮助下:http: //thesmithfam.org/blog/2009/10/13/cool-qprogressbar-stylesheet/

but I just wanted to change the color and not the progressbar itself. so I got rid of the first line, and change the second one a little bit.

但我只是想改变颜色而不是进度条本身。所以我去掉了第一行,并稍微改变了第二行。

Finally I got what I wanted.

最后我得到了我想要的。

First do this:

首先这样做:

QString danger = "QProgressBar::chunk {background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #FF0350,stop: 0.4999 #FF0020,stop: 0.5 #FF0019,stop: 1 #FF0000 );border-bottom-right-radius: 5px;border-bottom-left-radius: 5px;border: .px solid black;}";
QString safe= "QProgressBar::chunk {background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #78d,stop: 0.4999 #46a,stop: 0.5 #45a,stop: 1 #238 );border-bottom-right-radius: 7px;border-bottom-left-radius: 7px;border: 1px solid black;}";

Now all you have to do is:

现在你所要做的就是:

if(ui->progressbar->value()<80)
    ui->progressbar->setStyleSheet(danger);
else
    ui->progressbar->setStyleSheet(safe);

回答by user1894812

Using the "Highlight" color role does the trick in my case (using Plastique style).

在我的情况下,使用“突出显示”颜色角色可以解决问题(使用 Plastique 风格)。

QPalette p = palette();
p.setColor(QPalette::Highlight, Qt::green);
setPalette(p);