Is there circular QProgressbar with range (0,0) in qt?

社会主义新天地 提交于 2019-12-02 08:38:50

One day I wrote a simple class to implement a circular progress bar for my purposes (Qt 4.6). You can modify its appearance as you need. Hope it will help.

loading.h

#pragma once

#include <QtGui>

class Loading : public QWidget {
    Q_OBJECT
public:
    explicit Loading(QWidget *parent);

protected:
    double current;
    bool eventFilter(QObject *obj, QEvent *ev);
    bool event(QEvent *);
    void paintEvent(QPaintEvent *);
    void timerEvent(QTimerEvent *);
};

loading.cpp

#include "loading.h"

/**
 * @brief Creates a circular loading
 * @param parent - non-NULL widget that will be contain a circular loading
 */
Loading::Loading(QWidget *parent) : QWidget(parent), current(0) {
    Q_ASSERT(parent);
    parent->installEventFilter(this);
    raise();
    setAttribute(Qt::WA_TranslucentBackground);
    startTimer(20);
}

bool Loading::eventFilter(QObject *obj, QEvent *ev) {
    if (obj == parent()) {
        if (ev->type() == QEvent::Resize) {
            QResizeEvent * rev = static_cast<QResizeEvent*>(ev);
            resize(rev->size());
        }
        else if (ev->type() == QEvent::ChildAdded)
            raise();
    }
    return QWidget::eventFilter(obj, ev);
}

bool Loading::event(QEvent *ev) {
    if (ev->type() == QEvent::ParentAboutToChange) {
        if (parent()) parent()->removeEventFilter(this);
    }
    else if (ev->type() == QEvent::ParentChange) {
        if (parent()) {
            parent()->installEventFilter(this);
            raise();
        }
    }
    return QWidget::event(ev);
}

void Loading::paintEvent(QPaintEvent *) {
    QPainter p(this);
    p.fillRect(rect(), QColor(100, 100, 100, 64));

    QPen pen;
    pen.setWidth(12);
    pen.setColor(QColor(0, 191, 255));  // DeepSkyBlue
    p.setPen(pen);

    p.setRenderHint(QPainter::Antialiasing);

    QRectF rectangle(width()/2 - 40.0, height()/2 - 40.0, 80.0, 80.0);

    int spanAngle = (int)(qMin(0.2, current) * 360 * -16);
    int startAngle = (int)(qMin(0.0, 0.2 - current) * 360 * 16);

    p.drawArc(rectangle, startAngle, spanAngle);
}

void Loading::timerEvent(QTimerEvent *) {
    if (isVisible()) {
        current += 0.03;
        if (current > 1.2) current = 0.2; // restart cycle
        update();
    }
}

Usage

#include "loading.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    QLabel w;
    w.setMinimumSize(400, 400);
    Loading *loading = new Loading(&w);
    QTimer::singleShot(4000, loading, SLOT(hide()));
    w.show();
    return a.exec();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!