如何用Qt QPainter绘制线性梯度弧?

时间:2023-01-22 20:22:17

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

我正在尝试开发一个自定义的QProgressBar,它看起来像以下图片:

如何用Qt QPainter绘制线性梯度弧?

I created a class that extends QProgressBar and implemented the paintEvent() :

我创建了一个扩展QProgressBar的类并实现了paintEvent():

void CircularProgressBar::paintEvent(QPaintEvent*) {

int progress = this->value();
int progressInDegrees = (double)(progress*360)/100;

int barWidth = 20;

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);

painter.setPen(QPen(Qt::black, barWidth, Qt::SolidLine,Qt::RoundCap));

painter.drawArc(barWidth/2, barWidth/2, this->width() - barWidth, this->height() - barWidth,
                90*16, progressInDegrees*-16);}

This works great to draw the circular progress bar, but I'm having trouble with the linear gradient color of the bar. I tried creating a QPen with a QLinearGradient object and I tried setting the QPainter brush to a QLinearGradient object, but neither strategy worked. Is it possible to draw an arc with QPainter that has a linear gradient color?

这很好地画出了循环进度条,但是我遇到了线性渐变颜色的问题。我尝试用一个q线性渐变对象创建一个QPen,我尝试将QPainter画笔设置为一个QLinearGradient对象,但这两个策略都不起作用。是否可以用一个有线性渐变颜色的QPainter绘制一个圆弧?

2 个解决方案

#1


6  

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:

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

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:

widget.cpp:

#include "widget.h"

#include <QPaintEvent>
#include <QPainter>
#include <QConicalGradient>
#include <QPen>

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:

main.cpp:

#include "widget.h"
#include <QApplication>

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:

结果是:

如何用Qt QPainter绘制线性梯度弧?

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.

当然,这并不是一个完整准确的解决方案,但我认为,为了实现你想要的,你需要知道的一切。其余的细节并不难实现。

#2


2  

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

这个解决方案不是你想要的;梯度从上到下,而不是围绕着圆:

#include <QtWidgets>

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

然而,它是一个线性渐变的弧线!:p

#1


6  

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:

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

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:

widget.cpp:

#include "widget.h"

#include <QPaintEvent>
#include <QPainter>
#include <QConicalGradient>
#include <QPen>

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:

main.cpp:

#include "widget.h"
#include <QApplication>

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:

结果是:

如何用Qt QPainter绘制线性梯度弧?

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.

当然,这并不是一个完整准确的解决方案,但我认为,为了实现你想要的,你需要知道的一切。其余的细节并不难实现。

#2


2  

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

这个解决方案不是你想要的;梯度从上到下,而不是围绕着圆:

#include <QtWidgets>

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

然而,它是一个线性渐变的弧线!:p