How to draw a linear gradient arc with Qt QPainter?

后端 未结 2 1290
你的背包
你的背包 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:16

    I know this is an old question but I came across it some days ago and I think I have a solution. What you want is to create a conical gradient and clip the disk you want to use as circular loading bar. Here is an example:

    widget.h:

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include 
    
    class QPaintEvent;
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Widget(QWidget *parent = 0);
        ~Widget();
    
        void setLoadingAngle(int loadingAngle);
        int loadingAngle() const;
    
        void setDiscWidth(int width);
        int discWidth() const;
    
    protected:
        void paintEvent(QPaintEvent *);
    
    private:
        int m_loadingAngle;
        int m_width;
    };
    
    #endif // WIDGET_H
    

    widget.cpp:

    #include "widget.h"
    
    #include 
    #include 
    #include 
    #include 
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        m_loadingAngle(0),
        m_width(0)
    {
    }
    
    Widget::~Widget()
    {
    }
    
    void Widget::setLoadingAngle(int loadingAngle)
    {
        m_loadingAngle = loadingAngle;
    }
    
    int Widget::loadingAngle() const
    {
        return m_loadingAngle;
    }
    
    void Widget::setDiscWidth(int width)
    {
        m_width = width;
    }
    
    int Widget::discWidth() const
    {
        return m_width;
    }
    
    void Widget::paintEvent(QPaintEvent *)
    {
        QRect drawingRect;
        drawingRect.setX(rect().x() + m_width);
        drawingRect.setY(rect().y() + m_width);
        drawingRect.setWidth(rect().width() - m_width * 2);
        drawingRect.setHeight(rect().height() - m_width * 2);
    
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
    
        QConicalGradient gradient;
        gradient.setCenter(drawingRect.center());
        gradient.setAngle(90);
        gradient.setColorAt(0, QColor(178, 255, 246));
        gradient.setColorAt(1, QColor(5, 44, 50));
    
        int arcLengthApproximation = m_width + m_width / 3;
        QPen pen(QBrush(gradient), m_width);
        pen.setCapStyle(Qt::RoundCap);
        painter.setPen(pen);
        painter.drawArc(drawingRect, 90 * 16 - arcLengthApproximation, -m_loadingAngle * 16);
    }
    

    main.cpp:

    #include "widget.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        Widget w;
        w.setDiscWidth(20);
        w.setLoadingAngle(270);
        w.show();
    
        return a.exec();
    }
    

    And the result is:

    enter image description here

    Of course, it is not the complete and exact solution but I think it is everything you need to know in order to achieve what you want. The rest are details not hard to implement.

提交回复
热议问题