Gradient for chunks in QProgressBar

2019-07-18 02:50发布

Is it possible to set a common gradient for all QProgressBar chunks?

If use something like this:

QProgressBar::chunk:horizontal {
background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, 
                            stop: 0 green, 
                            stop: 1 white);
margin-right: 2px;    
width: 10px;
}

the result will be

http://labs.trolltech.com/blogs/wp-content/uploads/2007/06/progressbar_righttext.png

but I want to obtain a one gradient, stretched to all chunks. Like this:

http://labs.trolltech.com/blogs/wp-content/uploads/2007/06/progressbar_nochunk.png

divided onto chunks.

Thanks for all!

3条回答
对你真心纯属浪费
2楼-- · 2019-07-18 03:37

You must only remove:

QProgressBar::chunk:horizontal {
    background: qlineargradient(x1: 0,
                                y1: 0.5,
                                x2: 1,
                                y2: 0.5,
                                stop: 0 green,
                                stop: 1 white);
    margin-right: 2px;
    width: 10px; // <------ remove this propierty
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-18 03:37

something like this would work, but I'd prefer subclassing QProgressBar as webclectic said

class Wrapper : public QWidget
{
    Q_OBJECT

        QProgressBar *progressBar ;
        QSlider *slider ;

    public :

        Wrapper(void) : QWidget(), progressBar(new QProgressBar), slider(new QSlider(Qt::Horizontal))
        {
            progressBar->setMinimum(0) ;
            progressBar->setMaximum(100) ;
            slider->setMinimum(0) ;
            slider->setMaximum(100) ;
            QVBoxLayout *l = new QVBoxLayout ;
            setLayout(l) ;
            l->addWidget(progressBar) ;
            l->addWidget(slider) ;
            slider->setValue(0) ;
            connect(slider, SIGNAL(valueChanged(int)), SLOT(slider_value_changed(int))) ;
            slider_value_changed(0) ;
        }

    protected slots :

        void slider_value_changed(int new_value)
        {
            QString updated_bg = QString("background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0.0 green, stop: %0 white, stop: 1.0 white);").arg(new_value/100.0) ;
            QString style_sheet ;
            style_sheet +=  QString("QProgressBar {"
                            "%0"
                            "border: 2px solid grey;"
                            "border-radius: 5px;"
                            "text-align: center;"
                            "}").arg(updated_bg) ;
            style_sheet +=  "QProgressBar::chunk {"
                            "background: transparent;"
                            "width: 10px;"
                            "margin: 0.5px;"
                            "}" ;
            progressBar->setStyleSheet(style_sheet) ;
            progressBar->setValue(new_value) ;
        }
} ;

int main( int argc, char **argv )
{
    QApplication app(argc, argv) ;

    Wrapper w ;

    w.show() ;

    return app.exec() ;
}
查看更多
小情绪 Triste *
4楼-- · 2019-07-18 03:52

You cannot achieve what you want with the existing stylesheet properties. You could however subclass QProgressBar and reimplement the paint in order to get the appearance you wish.

查看更多
登录 后发表回答