本控件也非原創控件,是參考網上的代碼而來的,對稱顧名思義就是將畫布平均成上下兩部分,將設置的值自動按照畫布高度的一半做爲參照高度進行繪製,而後增長動態過渡效果,有點相似於聲音播放時候的頻譜效果,通常都會用多個直方對稱圖組合成一個控件來實現多個效果,看起來會更美觀,背景顏色能夠設置成漸變的,柱狀條的顏色也能夠自行設置。linux
#ifndef WAVEDOUBLE_H #define WAVEDOUBLE_H /** * 直方對稱圖控件 做者:feiyangqingyun(QQ:517216493) 2016-11-6 * 1:可設置最大值/最小值/當前值 * 2:可設置每次過渡的步長 * 3:可設置item之間的間隔 * 4:可設置漸變的背景顏色 * 5:可設置柱狀條的顏色 */ #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 WaveDouble : public QWidget #else class WaveDouble : 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(QColor bgColorStart READ getBgColorStart WRITE setBgColorStart) Q_PROPERTY(QColor bgColorEnd READ getBgColorEnd WRITE setBgColorEnd) Q_PROPERTY(QColor barColor READ getBarColor WRITE setBarColor) public: explicit WaveDouble(QWidget *parent = 0); ~WaveDouble(); protected: void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawBar(QPainter *painter); private: int minValue; //最小值 int maxValue; //最大值 int value; //目標值 int step; //步長 int space; //間距 QColor bgColorStart; //背景漸變開始顏色 QColor bgColorEnd; //背景漸變結束顏色 QColor barColor; //柱狀條顏色 double currentValue; //當前值 bool reverse; //是否倒退 QTimer *timer; //繪製定時器 private slots: void updateValue(); void stop(); public: int getMinValue() const; int getMaxValue() const; int getValue() const; int getStep() const; int getSpace() 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(int step); //設置間距 void setSpace(int space); //設置背景顏色 void setBgColorStart(const QColor &bgColorStart); void setBgColorEnd(const QColor &bgColorEnd); //設置柱狀條顏色 void setBarColor(const QColor &barColor); Q_SIGNALS: void valueChanged(int value); }; #endif // WAVEDOUBLE_H
void WaveDouble::paintEvent(QPaintEvent *) { //繪製準備工做,啓用反鋸齒 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //繪製漸變背景 drawBg(&painter); //繪製柱狀條塊 drawBar(&painter); } void WaveDouble::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 WaveDouble::drawBar(QPainter *painter) { painter->save(); painter->setPen(Qt::NoPen); painter->setBrush(barColor); //找到中心點Y軸座標 double centerY = (double) height() / 2; //每一格遞增量 double increment = (double)(height() - 2 * space) / (maxValue - minValue); //找到當前值的起始和結束Y軸座標 上下兩半因此 /2 double startY = centerY - (currentValue / 2) * increment; double endY = centerY + (currentValue / 2) * increment; QRectF barRect(QPointF(space, startY), QPointF(width() - space, endY)); painter->drawRect(barRect); painter->restore(); }