有qt中有范围(0,0)的循环QProgressbar吗? [英] Is there circular QProgressbar with range (0,0) in qt?
本文介绍了有qt中有范围(0,0)的循环QProgressbar吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要有循环 QProgressbar
,它的外观必须像正常 QProgressbar
I want to have circular QProgressbar
which it's appearance must look like the normal QProgressbar
with the Range between 0 and 0.
线性 QProgressbar
的代码如下:/ p>
The code for the linear QProgressbar
is some thing like below:
QProgressBar *_progressBar = new QProgressBar();
_progressBar->setTextVisible(false);
_progressBar->setRange(0, 0);
但我想要它是圆形的。有没有办法在qt中实现它?
but I want it to be circular. Is there any way to implement it in qt?
推荐答案
有一天我写了一个简单的类来实现一个循环进度条目的(Qt 4.6)。您可以根据需要修改其外观。希望它会有所帮助。
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.
#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
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();
}
这篇关于有qt中有范围(0,0)的循环QProgressbar吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文