波浪曲線控件,實際上是以前一個水波進度條控件的一個核心,其實就是利用正弦曲線來生成對應的座標進行繪製,把這個功能單獨提取出來,是爲了更詳細的研究各類正弦餘弦等拓展效果,當時寫這個效果的時候,參考的是網上android繪製的水波效果的代碼,而後本身從新理解之後,整理成Qt的版本,拓展了部分效果好比能夠設置浪的高度,浪的密度,密度越大越浪,^_^,演示控件效果提供了滑塊來設置對應的參數。linux
#ifndef WAVEPLOT_H #define WAVEPLOT_H /** * 正弦曲線控件 做者:feiyangqingyun(QQ:517216493) 2017-2-10 * 1:可設置波浪的速度 * 2:可設置波浪的高度 * 3:可設置波浪的密度,密度越大越浪 * 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 WavePlot : public QWidget #else class WavePlot : public QWidget #endif { Q_OBJECT Q_PROPERTY(int interval READ getInterval WRITE setInterval) Q_PROPERTY(double waterHeight READ getWaterHeight WRITE setWaterHeight) Q_PROPERTY(double waterDensity READ getWaterDensity WRITE setWaterDensity) Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor) Q_PROPERTY(QColor bgColor READ getBgColor WRITE setBgColor) public: explicit WavePlot(QWidget *parent = 0); ~WavePlot(); protected: void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawLine(QPainter *painter); private: int interval; //間隔 double waterHeight; //水波高度 double waterDensity; //水波密度 QColor lineColor; //線條顏色 QColor bgColor; //背景顏色 double offset; //水波偏移量 QTimer *timer; //定時器繪製動畫 public: int getInterval() const; double getWaterHeight() const; double getWaterDensity() const; QColor getLineColor() const; QColor getBgColor() const; QSize sizeHint() const; QSize minimumSizeHint() const; public Q_SLOTS: //啓動波動 void start(); //中止波動 void stop(); //設置波動間隔 void setInterval(int interval); //設置水波高度 void setWaterHeight(double waterHeight); //設置水波密度 void setWaterDensity(double waterDensity); //設置線條顏色 void setLineColor(const QColor &lineColor); //設置背景顏色 void setBgColor(const QColor &bgColor); }; #endif // WAVEPLOT_H
void WavePlot::paintEvent(QPaintEvent *) { //繪製準備工做,啓用反鋸齒 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); //繪製背景 drawBg(&painter); //繪製波紋 drawLine(&painter); } void WavePlot::drawBg(QPainter *painter) { painter->save(); painter->setBrush(bgColor); painter->drawRect(this->rect()); painter->restore(); } void WavePlot::drawLine(QPainter *painter) { int height = this-> height(); int width = this->width(); //起始點座標和結束點座標 int startX = 0; int startY = 0; int endX = width - 0; int endY = height - 0; //計算當前值所佔百分比 double percent = 0.5; //正弦曲線公式 y = A * qSin(ωx + φ) + k //w表示週期,能夠理解爲水波的密度,值越大密度越大(浪越密集 ^_^),取值 密度*M_PI/寬度 double w = waterDensity * M_PI / endX; //A表示振幅,能夠理解爲水波的高度,值越大高度越高(越浪 ^_^),取值高度的百分比 double A = endY * waterHeight; //k表示y軸偏移,能夠理解爲進度,取值高度的進度百分比 double k = endY * percent; //第一條波浪路徑集合 QPainterPath waterPath1; //第二條波浪路徑集合 QPainterPath waterPath2; //移動到左上角起始點 waterPath1.moveTo(startX, endY); waterPath2.moveTo(startX, endY); offset += 0.6; if (offset > (endX / 2)) { offset = 0; } for(int x = startX; x <= endX; x++) { //第一條波浪Y軸 double waterY1 = (double)(A * qSin(w * x + offset)) + k; //第二條波浪Y軸 double waterY2 = (double)(A * qSin(w * x + offset + (endX / 2 * w))) + k; //將兩個點鏈接成線 waterPath1.lineTo(x, waterY1); waterPath2.lineTo(x, waterY2); } //移動到右下角結束點,總體造成一個閉合路徑 waterPath1.lineTo(endX, endY); waterPath2.lineTo(endX, endY); QColor waterColor1 = lineColor; waterColor1.setAlpha(100); QColor waterColor2 = lineColor; waterColor2.setAlpha(200); painter->save(); painter->setPen(Qt::NoPen); //第一條波浪路徑 painter->setBrush(waterColor1); painter->drawPath(waterPath1); //第二條波浪路徑 painter->setBrush(waterColor2); painter->drawPath(waterPath2); painter->restore(); }