QT 实现固定大小圆形进度条

#ifndef MYCIRPROCESS_H
#define MYCIRPROCESS_H
 
#include <QtCore>
#include <QtGui>
#include <qwidget.h>
#include <QLabel>
#include <QApplication>
#include <qdesktopwidget.h>
 
class StorageDisplay : public QWidget
{
    Q_OBJECT
public:
    StorageDisplay(QWidget *parent = 0);
    ~StorageDisplay();
    void setUsedValue(int value);
    //void setSize(int width, int height);
protected:
    void paintEvent(QPaintEvent *event);
 //   void resizeEvent (QResizeEvent * event);
//    void mousePressEvent(QMouseEvent *event);
//    void mouseMoveEvent(QMouseEvent *event);
//    void mouseReleaseEvent(QMouseEvent *event);
//    void showEvent(QShowEvent *event);
//    void hideEvent(QHideEvent *event);
 
private slots:
 //   void slotUpdateTimer();
 
private:
    QPoint beginDrag;
    bool bPressFlag;
 
    QPixmap backGround;
    int userdVaule;
    int currentValue;
    QLabel *startValueLabel;
    QLabel *endValueLabel;
    QLabel *dispayValueLabel;
 
 //   QTimer *updateTimer;
};
 

#endif // MYCIRPROCESS_H

#include "storagedisplay.h"
 
StorageDisplay::StorageDisplay(QWidget *parent)
    : QWidget(parent, Qt::FramelessWindowHint)
    , bPressFlag(false)
    , currentValue(0)
{
    //resize(300, 300);
    setAutoFillBackground(false);
    QPalette pal = palette();
    pal.setColor(QPalette::Background, QColor(0xFF,0xFF,0xFF,0xFF));
    setPalette(pal);
 
//    startValueLabel = new QLabel(tr("0%"), this);
//    startValueLabel->setFont(QFont("Arial", 11, QFont::Normal));
//    startValueLabel->setStyleSheet("color:#898989");
 
//    endValueLabel = new QLabel(tr("100%"),this);
//    endValueLabel->setFont(QFont("Arial", 11, QFont::Normal));
//    endValueLabel->setStyleSheet("color:#898989");
 
    dispayValueLabel = new QLabel(this);
    dispayValueLabel->setStyleSheet("color:#349BDA");
 
//    updateTimer = new QTimer(this);
//    updateTimer->setInterval(20);
//    connect(updateTimer, SIGNAL(timeout()), this, SLOT(slotUpdateTimer()));
}
 
StorageDisplay::~StorageDisplay()
{
//    if (updateTimer->isActive()) {
//        updateTimer->stop();
//    }
    currentValue = 0;
}
 
void StorageDisplay::setUsedValue(int value)
{
    if(value >= 100)
    {
        currentValue = 100;
    }
    else
    {
        currentValue = value;
    }
 
    update();
}
 
//void StorageDisplay::showEvent(QShowEvent *event)
//{
//    updateTimer->start();
//    currentValue = 0;
//}
 
//void StorageDisplay::hideEvent(QHideEvent *event)
//{
//    if (updateTimer->isActive()) {
//        updateTimer->stop();
//    }
//    currentValue = 0;
//}
 
//void StorageDisplay::slotUpdateTimer()
//{
//    if (currentValue >= userdVaule) {
//        updateTimer->stop();
//        return;
//    }
//    currentValue++;
//    update();
//}
 
void StorageDisplay::paintEvent(QPaintEvent *event)
{
 
    QPainter painter(this);
    QColor usedColor(165, 220, 62);
    QColor freeColor(215, 215, 215);
    painter.drawPixmap(QRect((width() - backGround.width())/2 , (height() - backGround.height())/2, backGround.width(), backGround.height()) , backGround);
 
    painter.translate(width() / 2, height() / 2);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.save();
    painter.rotate(42);
    painter.setPen(QPen(usedColor, 2));
    for (int i = 0; i < currentValue ; ++i) {
        painter.drawLine(0, 70, 0, 80);
        painter.rotate(2.8);
    }
    painter.setPen(QPen(freeColor, 3));
    for (int i = currentValue; i < 100 ; ++i) {
        painter.drawLine(0, 70, 0, 80);
        painter.rotate(2.8);
    }
    if (currentValue == 0) {
        dispayValueLabel->setFont(QFont("Arial", 12, QFont::Bold));
        dispayValueLabel->setText(tr("0%"));
    }
    else {
        dispayValueLabel->setFont(QFont("Arial", 15, QFont::Bold));
        dispayValueLabel->setText(tr("%1%").arg(currentValue));
    }
    QFontMetrics metrics(dispayValueLabel->font());
    int textwidth = metrics.width(dispayValueLabel->text());
    int textheight = metrics.height();
    dispayValueLabel->setGeometry((width() - textwidth)/2, (height() - textheight)/2 , textwidth, textheight);
    painter.restore();
 
    painter.translate(-width()/2, -height()/2);
    painter.setBrush(QColor(233, 233, 233));
    painter.setPen(QPen(QColor(233, 233, 233), 15));
    painter.drawEllipse(QRectF((width()/2 - 55), (height()/2 - 55), 110, 110));
 
    QConicalGradient conicalGradient(width()/2, height()/2, 90);
    conicalGradient.setColorAt(0, QColor(45, 204, 112));
    conicalGradient.setColorAt(1.0, QColor(51, 152, 219));
    painter.setPen(QPen(QBrush(conicalGradient), 30));
    painter.drawEllipse(QRectF((width()/2 - 35), (height()/2 - 35), 70, 70));
 
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(249, 249, 249));
    painter.drawEllipse(QRectF((width()/2 - 30), (height()/2 - 30), 60, 60));
}
 
//void StorageDisplay::resizeEvent(QResizeEvent *event)
//{
//    move((QApplication::desktop()->width() - width())/2,  (QApplication::desktop()->height() - height())/2);
//    startValueLabel->setGeometry(35, 140, 25, 20);
//    endValueLabel->setGeometry(97, 140, 50, 20);
//}
 
/****************move everywhere*******************/
//void StorageDisplay::mousePressEvent(QMouseEvent *event)
//{
//    bPressFlag = true;
//    beginDrag = event->pos();
//    QWidget::mousePressEvent(event);
//}
 
//void StorageDisplay::mouseMoveEvent(QMouseEvent *event)
//{
//    if (bPressFlag) {
//        QPoint relaPos(QCursor::pos() - beginDrag);
//        move(relaPos);
//    }
//    QWidget::mouseMoveEvent(event);
//}
 
//void StorageDisplay::mouseReleaseEvent(QMouseEvent *event)
//{
//    bPressFlag = false;
//    QWidget::mouseReleaseEvent(event);
//}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值