直方波形圖控件非原創控件,控件大全中大概有20-30個控件非本身原創,而是參考了網上開源的代碼,本身加以整理和完善,新增了插件的代碼使得能夠直接集成到QtDesigner或者QtCreator中。直方波形圖,主要就是將外部傳入的座標集合數據進行漸變過渡的繪製,產生一個動態的過渡效果,將設置的座標集合從新運算+1變成新的座標集合來繪製,這樣看起來繪製不會很死,而是緩慢的過渡。linux
#ifndef WAVELINE_H #define WAVELINE_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 WaveLine : public QWidget #else class WaveLine : public QWidget #endif { Q_OBJECT Q_PROPERTY(int maxValue READ getMaxValue WRITE setMaxValue) Q_PROPERTY(int 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 lineColor READ getLineColor WRITE setLineColor) public: explicit WaveLine(QWidget *parent = 0); ~WaveLine(); protected: void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawLine(QPainter *painter); private: int maxValue; //最大值 int step; //步長 int space; //間距 QColor bgColorStart; //背景漸變開始顏色 QColor bgColorEnd; //背景漸變結束顏色 QColor lineColor; //線條顏色 QTimer *timer; //繪製定時器 QVector<int> currentDataVec; //當前數據集合 QVector<int> dataVec; //目標數據集合 private slots: void updateData(); public: int getMaxValue() const; int getStep() const; int getSpace() const; QColor getBgColorStart() const; QColor getBgColorEnd() const; QColor getLineColor() const; QSize sizeHint() const; QSize minimumSizeHint() const; public Q_SLOTS: //設置數據 void setData(const QVector<int> &dataVec); //設置最大值 void setMaxValue(int maxValue); //設置步長 void setStep(int step); //設置間距 void setSpace(int space); //設置背景顏色 void setBgColorStart(const QColor &bgColorStart); void setBgColorEnd(const QColor &bgColorEnd); //設置線條顏色 void setLineColor(const QColor &lineColor); }; #endif // WAVELINE_H
void WaveLine::paintEvent(QPaintEvent *) { //繪製準備工做,啓用反鋸齒 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //繪製背景 drawBg(&painter); //繪製線條 drawLine(&painter); } void WaveLine::drawBg(QPainter *painter) { painter->save(); painter->setPen(Qt::NoPen); QLinearGradient bgGradient(QPoint(0, 0), QPoint(0, height())); bgGradient.setColorAt(0.0, bgColorStart); bgGradient.setColorAt(1.0, bgColorEnd); painter->setBrush(bgGradient); painter->drawRect(rect()); painter->restore(); } void WaveLine::drawLine(QPainter *painter) { painter->save(); painter->setPen(QPen(lineColor, 2)); int count = dataVec.count(); double increment = (double)width() / count; double initX = 0; QVector<QPointF> pointVec; for (int i = 0; i < count - 1; i++) { double currentValue = currentDataVec.at(i); double y1 = height() - (double)height() / maxValue * currentValue; double nextValue = currentDataVec.at(i + 1); double y2 = height() - (double)height() / maxValue * nextValue; QPointF point1(initX, y1); QPointF point2(initX + increment, y2); initX += increment; pointVec.append(point1); pointVec.append(point2); } painter->drawLines(pointVec); painter->restore(); }