How to draw a linear gradient arc with Qt QPainter?

后端 未结 2 1264
你的背包
你的背包 2021-01-11 19:39

I\'m trying to develop a custom QProgressBar that will look like the following image :

\"enter

2条回答
  •  孤街浪徒
    2021-01-11 20:08

    This solution is not exactly what you're after; the gradient goes from top to bottom, rather than around the circle:

    #include 
    
    class Widget : public QWidget
    {
    public:
        Widget() {
            resize(200, 200);
        }
    
        void paintEvent(QPaintEvent *) {
            QPainter painter(this);
            painter.setRenderHint(QPainter::Antialiasing);
    
            const QRectF bounds(0, 0, width(), height());
            painter.fillRect(bounds, "#1c1c1c");
    
            QPen pen;
            pen.setCapStyle(Qt::RoundCap);
            pen.setWidth(20);
    
            QLinearGradient gradient;
            gradient.setStart(bounds.width() / 2, 0);
            gradient.setFinalStop(bounds.width() / 2, bounds.height());
            gradient.setColorAt(0, "#1c1c1c");
            gradient.setColorAt(1, "#28ecd6");
    
            QBrush brush(gradient);
            pen.setBrush(brush);
            painter.setPen(pen);
    
            QRectF rect = QRectF(pen.widthF() / 2.0, pen.widthF() / 2.0, width() - pen.widthF(), height() - pen.widthF());
            painter.drawArc(rect, 90 * 16, 0.65 * -360 * 16);
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        Widget w;
        w.show();
    
        return app.exec();
    }
    

    However, it is an arc with a linear gradient! :p

提交回复
热议问题