直方動態圖相似於音樂播放時候的柱狀圖展現,頂部提供一個橫線條,當柱狀上升的時候,該線條相似於帽子的形式衝到頂端,至關於柱狀頂上去的感受,給人一種動態的感受,聽音樂的同時更加賞心悅目,原理比較簡單,就是用2個定時器,一個定時器間隔比較短,負責快速把柱狀圖從底部衝到設置的值,同時橫線條跟隨一塊兒衝上去,一個定時器負責慢慢的跌落值到0,而後橫線條緩慢降低,降低速度比柱狀圖的速度要慢一些,產生一種對比的效果,看起來更像是跌落的感受。linux
#ifndef WAVEBAR_H #define WAVEBAR_H /** * 直方動態圖控件 做者:feiyangqingyun(QQ:517216493) 2016-11-6 * 1:可設置最大值/最小值/當前值 * 2:可設置頂部滑塊高度 * 3:可設置每次變更的步長 * 4:可設置item之間的間隔 * 5:可設置漸變的背景顏色 * 6:可設置柱狀條顏色 */ #include <QWidget> #ifdef quc #if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) #include <QtDesigner/QDesignerExportWidget> #else #include <QtUiPlugin/QDesignerExportWidget> #endif class QDESIGNER_WIDGET_EXPORT WaveBar : public QWidget #else class WaveBar : public QWidget #endif { Q_OBJECT Q_PROPERTY(int minValue READ getMinValue WRITE setMinValue) Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue) Q_PROPERTY(int value READ getValue WRITE setValue) Q_PROPERTY(double step READ getStep WRITE setStep) Q_PROPERTY(int space READ getSpace WRITE setSpace) Q_PROPERTY(int headHeight READ getHeadHeight WRITE setHeadHeight) Q_PROPERTY(QColor bgColorStart READ getBgColorStart WRITE setBgColorStart) Q_PROPERTY(QColor bgColorEnd READ getBgColorEnd WRITE setBgColorEnd) Q_PROPERTY(QColor barColor READ getBarColor WRITE setBarColor) public: explicit WaveBar(QWidget *parent = 0); ~WaveBar(); protected: void resizeEvent(QResizeEvent *); void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawBar(QPainter *painter); void drawHead(QPainter *painter); private: int minValue; //最小值 int maxValue; //最大值 int value; //目標值 double step; //步長 int space; //間距 int headHeight; //頂部條塊高度 QColor bgColorStart; //背景漸變開始顏色 QColor bgColorEnd; //背景漸變結束顏色 QColor barColor; //柱狀條顏色 int currentValue; //當前值 double headY; //頂部條塊Y軸座標 double barY; //柱狀條塊Y軸座標 QTimer *barTimer; //柱狀條塊降低定時器 QTimer *headTimer; //頂部條塊下墜定時器 private slots: void updateBar(); void updateHead(); public: int getMinValue() const; int getMaxValue() const; int getValue() const; double getStep() const; int getSpace() const; int getHeadHeight() const; QColor getBgColorStart() const; QColor getBgColorEnd() const; QColor getBarColor() const; QSize sizeHint() const; QSize minimumSizeHint() const; public Q_SLOTS: //設置範圍值 void setRange(int minValue, int maxValue); //設置最大最小值 void setMinValue(int minValue); void setMaxValue(int maxValue); //設置目標值 void setValue(int value); //設置步長 void setStep(double step); //設置間距 void setSpace(int space); //設置頂部條塊高度 void setHeadHeight(int headHeight); //設置背景顏色 void setBgColorStart(const QColor &bgColorStart); void setBgColorEnd(const QColor &bgColorEnd); //設置柱狀條顏色 void setBarColor(const QColor &barColor); }; #endif // WAVEBAR_H
void WaveBar::paintEvent(QPaintEvent *) { //繪製準備工做,啓用反鋸齒 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //繪製漸變背景 drawBg(&painter); //繪製柱狀條塊 drawBar(&painter); //繪製頂部條塊 drawHead(&painter); } void WaveBar::drawBg(QPainter *painter) { painter->save(); painter->setPen(Qt::NoPen); QLinearGradient bgGradient(QPointF(0, 0), QPointF(0, height())); bgGradient.setColorAt(0.0, bgColorStart); bgGradient.setColorAt(1.0, bgColorEnd); painter->setBrush(bgGradient); painter->drawRect(rect()); painter->restore(); } void WaveBar::drawBar(QPainter *painter) { painter->save(); QRectF barRect(QPointF(space, barY), QPointF(width() - space, height() - space)); painter->setPen(Qt::NoPen); painter->setBrush(barColor); painter->drawRect(barRect); painter->restore(); } void WaveBar::drawHead(QPainter *painter) { painter->save(); QRectF headRect(QPointF(space, headY), QPointF(width() - space, headY - headHeight)); painter->setPen(Qt::NoPen); painter->setBrush(barColor); painter->drawRect(headRect); painter->restore(); } void WaveBar::updateBar() { barY += step; //超過底部則中止 int bottomY = height() - space - headHeight; if (barY >= bottomY) { if (barTimer->isActive()) { barTimer->stop(); } barY = bottomY; } this->update(); } void WaveBar::updateHead() { headY += step; //超過底部則中止 int bottomY = height() - space; if (headY >= bottomY) { if (headTimer->isActive()) { headTimer->stop(); } headY = bottomY; } this->update(); }