Gradient for chunks in QProgressBar

落花浮王杯 提交于 2019-12-12 16:25:34

问题


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!


回答1:


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.




回答2:


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:


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


来源:https://stackoverflow.com/questions/8354345/gradient-for-chunks-in-qprogressbar

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