Changing the fore color of a progress bar



  • Hi All,

    Simply, I'd like to know if there is a "style" color which I can change to change the fore-color on a progress bar. I'm able to change the background-color of my progress bar but event though I've changed the also the fore-core it didn't change.
    For "fore-color" I mean the color of the of the colored progressive bar.

    I hope it is clear what I need to know.....

    Thank you in advance.



  • I think what your looking for is "QProgressBar::chunk". The following example shows a QProgressBar that changes colour every 20% of its progress:

    @
    #include <QtGui>

    class ProgressBar : public QProgressBar {
    public:
    ProgressBar(QWidget *parent = 0) : QProgressBar(parent) {
    style="QProgressBar {"
    "text-align: center;"
    "}"
    "QProgressBar::chunk {"
    "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 %1, stop:1 %2);"
    "text-align: center;"
    "margin: 2px 1px 1p 2px;"
    "}";
    setRange(0,100);
    }

    public slots:
    void setValue(int value){
    QString c,ac;
    switch(value/20){
    case 0:
    c="#ff0000";
    break;
    case 1:
    c="#00ff00";
    break;
    case 2:
    c="#0000ff";
    break;
    case 3:
    c="#ffff00";
    break;
    case 4:
    c="#ff00ff";
    break;
    case 5:
    c="#000000";
    break;
    }
    ac=adjust(c,80);
    setStyleSheet(style.arg(ac).arg(c));
    QProgressBar::setValue(value);
    }

    private:
    QString adjust(QString color, int factor){
    QColor *c=new QColor(color);
    c->setHsl(c->hslHue(),
    c->hslSaturation(),
    qMin(c->lightness()+factor, 255));
    return c->name();
    }

    QString style;
    

    };

    class Widget : public QWidget {
    public:
    Widget(QWidget *parent = 0) : QWidget(parent), value(0) {
    QVBoxLayout *l=new QVBoxLayout(this);
    progress=new ProgressBar(this);
    progress->setValue(value);
    l->addWidget(progress);
    startTimer(100);
    }

    protected:
    void timerEvent(QTimerEvent *){
    value=value<100 ? value+1 : 0;
    progress->setValue(value);
    }

    private:
    ProgressBar *progress;
    int value;
    };

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
    }
    @

    The important bits are lines 9 thru 13. Obviously there is no need to subclass QProgressBar just to do this. Also, I use a linear gradient as the background to the chunk as I think a flat colour looks pretty awful, but that's up to you ;o)

    Hope this helps.



  • Thank you, it was very helpful.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.