直方波形图控件非原创控件,控件大全中大概有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(); }