문제

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!

도움이 되었습니까?

해결책

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.

다른 팁

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
}

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() ;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top