一、简介
Qt下进度条一般都是水平或垂直的,有时需要一个椭圆或圆来动态显示进度,或用此来显示存储百分比,都是比较适用的。
二、详解
1、代码
(1)widgetdisplay.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QtCore>
#include <QtGui>
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 // WIDGET_H
(2)widgetdisplay.cpp
#include "widgetdisplay.h"StorageDisplay::StorageDisplay(QWidget *parent) : QWidget(parent, Qt::FramelessWindowHint) , bPressFlag(false) , currentValue(0){ QTextCodec *codec = QTextCodec::codecForName("utf8"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); resize(167, 167); 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){ userdVaule = value;}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("unconfig")); } 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);}
</pre><pre>
(3)main.cpp
#include "widgetdisplay.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); StorageDisplay w; w.setUsedValue(100); w.show(); return a.exec();}
(4)编译运行
三、总结
(1)若有问题或建议,请留言,在此感谢!