Qt、QCustomPlot、实时波形绘制、实时曲线绘制
资源内容介绍
讲解见博客:https://blog.csdn.net/weixin_47488212/article/details/129299987 对于初学者而言,想要及时地做出一套上位机或实时波形显示界面,还是存在一定难度的,为了降低初学者的学习难度,亦方便其他研发人员的使用,笔者分享一套使用简单、功能强大的实时波形绘制控件,其基于Qt5与QCustomPlot实现。 在项目中必须包含QCustomPlot相关文件,笔者这里是直接包含qcustomplot.cpp、qcustomplot.h两个文件。另外在项目的.pro中,必须包含以下这句: QT += widgets printsupport 可以使用代码直接实例化WidgetPlot2D,或通过窗口提升,然后使用WidgetPlot2D绘制实时波形只需两步: ① 初始化波形名称:函数initGraphName(QStringList) ② 给对应的波形添加数据:函数addData(QString, double) /***************************************************************************** **** QCustomPlot, an easy to use, modern plotting widget for Qt **** Copyright (C) 2011-2021 Emanuel Eichhammer **** **** This program is free software: you can redistribute it and/or modify **** it under the terms of the GNU General Public License as published by **** the Free Software Foundation, either version 3 of the License, or **** (at your option) any later version. **** **** This program is distributed in the hope that it will be useful, **** but WITHOUT ANY WARRANTY; without even the implied warranty of **** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **** GNU General Public License for more details. **** **** You should have received a copy of the GNU General Public License **** along with this program. If not, see http://www.gnu.org/licenses/. **** ******************************************************************************** Author: Emanuel Eichhammer **** Website/Contact: http://www.qcustomplot.com/ **** Date: 29.03.21 **** Version: 2.1.0 ******************************************************************************/#include "qcustomplot.h"//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// QCPVector2D////////////////////////////////////////////////////////////////////////////////////////////////////QCPVector2D::QCPVector2D() : mX(0), mY(0){}QCPVector2D::QCPVector2D(double x, double y) : mX(x), mY(y){}QCPVector2D::QCPVector2D(const QPoint &point) : mX(point.x()), mY(point.y()){}QCPVector2D::QCPVector2D(const QPointF &point) : mX(point.x()), mY(point.y()){}void QCPVector2D::normalize(){ if (mX == 0.0 && mY == 0.0) return; const double lenInv = 1.0/length(); mX *= lenInv; mY *= lenInv;}QCPVector2D QCPVector2D::normalized() const{ if (mX == 0.0 && mY == 0.0) return *this; const double lenInv = 1.0/length(); return QCPVector2D(mX*lenInv, mY*lenInv);}double QCPVector2D::distanceSquaredToLine(const QCPVector2D &start, const QCPVector2D &end) const{ const QCPVector2D v(end-start); const double vLengthSqr = v.lengthSquared(); if (!qFuzzyIsNull(vLengthSqr)) { const double mu = v.dot(*this-start)/vLengthSqr; if (mu < 0) return (*this-start).lengthSquared(); else if (mu > 1) return (*this-end).lengthSquared(); else return ((start + mu*v)-*this).lengthSquared(); } else return (*this-start).lengthSquared();}double QCPVector2D::distanceSquaredToLine(const QLineF &line) const{ return distanceSquaredToLine(QCPVector2D(line.p1()), QCPVector2D(line.p2()));}double QCPVector2D::distanceToStraightLine(const QCPVector2D &base, const QCPVector2D &direction) const{ return qAbs((*this-base).dot(direction.perpendicular()))/direction.length();}QCPVector2D &QCPVector2D::operator*=(double factor){ mX *= factor; mY *= factor; return *this;}QCPVector2D &QCPVector2D::operator/=(double divisor){ mX /= divisor; mY /= divisor; return *this;}QCPVector2D &QCPVector2D::operator+=(const QCPVector2D &vector){ mX += vector.mX; mY += vector.mY; return *this;}QCPVector2D &QCPVector2D::operator-=(const QCPVector2D