Change the color of a QProgressBar

五迷三道 提交于 2019-12-04 18:07:21

问题


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

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 this almost identical question. Unfortunately, I couldn't make it work and the documentation on customizing QProgressBars doesn't help me either, so I would be very grateful for any suggestions as to what I'm doing wrong.

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.

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.


回答1:


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

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?




回答2:


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/

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



回答3:


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

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


来源:https://stackoverflow.com/questions/9368824/change-the-color-of-a-qprogressbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!