最近在画二维曲线,所以制作了这个PostCurveManage曲线管理类,其中涵盖了大部分制作曲线的方法,大家可以参考一下。前期的准备需要前往我以前的博客观看。传送门
这是.h
#ifndef POSTCURVEMANAGE_H
#define POSTCURVEMANAGE_H
#include <QWidget>
/*****************************************************************************
* Add by zzq
* 2022/10/09
* 此类为曲线图类。主要是在widget中实现二维曲线的展示。
* 提供以下方法:
* 1.默认构造函数只创建一个曲线图,无数据;
* 2.默认构造函数1,创建一个曲线图且传入曲线txt路径;
* 3.初始化曲线图;
* 4.添加数据;
* 5.添加曲线1; 参数 : txt路径
* 6.添加曲线2; 参数 :text路径、曲线名称
* 7.添加曲线3; 参数 :text路径、x轴名称、y轴名称
* 8.添加曲线4; 参数 :添加曲线名称、x轴值、y轴值
* 9.添加曲线5; 参数 :添加曲线路径、x轴最小值取值范围、x轴最大值取值范围、y轴最小值取值范围、y轴最大值取值范围
******************************************************************************/
namespace Ui {
class PostCurveManage;
}
class PostCurveManage : public QWidget
{
Q_OBJECT
public:
/****************************************************************************
* Add by zzq
* 2022/10/09
* 默认构造函数
*****************************************************************************/
explicit PostCurveManage(QWidget *parent);
/****************************************************************************
* Add by zzq
* 2022/10/09
* 默认构造函数1
* 添加WidgetTitle 名称
*****************************************************************************/
explicit PostCurveManage(const QString& tname = nullptr,QWidget *parent = nullptr);
~PostCurveManage();
/****************************************************************************
* Add by zzq
* 2022/10/09
* 初始化二维曲线
*****************************************************************************/
void init();
/****************************************************************************
* Add by zzq
* 2022/10/09
* 显示二维曲线
*****************************************************************************/
void showCurve(QVector<double> x,QVector<double> y);
/****************************************************************************
* Add by zzq
* 2022/10/09
* 添加曲线 QString path
*****************************************************************************/
void addCurve(QString);
/****************************************************************************
* Add by zzq
* 2022/10/09
* 添加曲线文件路径path
* 曲线名称name
*****************************************************************************/
void addCurve(QString paht,QString name);
/****************************************************************************
* Add by zzq
* 2022/10/09
* 添加曲线文件路径
* 添加X坐标名称
* 添加Y点坐标名称
*****************************************************************************/
void addCurve(QString path,QString xname,QString yname);
/****************************************************************************
* Add by zzq
* 2022/10/09
* 添加曲线name
* 添加X点坐标
* 添加Y点坐标
*****************************************************************************/
void addCurve(QString name,QVector<double> x,QVector<double> y);
/****************************************************************************
* Add by zzq
* 2022/10/09
* 添加曲线路径
* 添加X轴取值范围
* 添加Y轴取值范围
*****************************************************************************/
void addCurve(QString path,int x1, int x2, int y1, int y2);
/****************************************************************************
* Add by zzq
* 2022/10/09
* 添加曲线名称
*****************************************************************************/
void setCurveName(QString);
/****************************************************************************
* Add by zzq
* 2022/10/09
* 设置x轴与y轴取值范围
*****************************************************************************/
void setShaftScope(int x1,int x2,int y1,int y2);
private:
Ui::PostCurveManage *ui;
///表格标题
QString _tname;
};
#endif // POSTCURVEMANAGE_H
这是.cpp
#include "PostCurveManage.h"
#include "ui_PostCurveManage.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
PostCurveManage::PostCurveManage(QWidget *parent) :
QWidget(parent),
ui(new Ui::PostCurveManage)
{
ui->setupUi(this);
ui->widget->addGraph();
}
PostCurveManage::PostCurveManage(const QString& tname ,QWidget *parent) :
QWidget(parent),
ui(new Ui::PostCurveManage),
_tname(tname)
{
ui->setupUi(this);
ui->widget->addGraph();
this->setWindowTitle(tname);
}
PostCurveManage::~PostCurveManage()
{
delete ui;
}
void PostCurveManage::init()
{
}
void PostCurveManage::showCurve(QVector<double> x,QVector<double> y)
{
ui->widget->graph(0)->setData(x, y);
// 设置坐标轴的范围,以看到所有数据
ui->widget->legend->setVisible(true); // 显示图例
//重画图像
ui->widget->replot();
}
void PostCurveManage::addCurve(QString path)
{
QVector<double> x;
QVector<double> y;
QFile txtPath(path);
if(!txtPath.open(QIODevice::ReadOnly))
{
qDebug()<<txtPath.errorString();
}
QTextStream textStream;
textStream.setDevice(&txtPath);
while(!textStream.atEnd())
{
QString str1 = textStream.readLine();//每次读取一行
QStringList str = str1.split("\t");
if(str.length() == 2){
x<<str[0].toDouble();
y<<str[1].toDouble();
}
}
txtPath.close();
showCurve(x,y);
}
void PostCurveManage::addCurve(QString path,QString name)
{
addCurve(path);
this->setWindowTitle(name);
}
void PostCurveManage::addCurve(QString path,QString xname,QString yname)
{
addCurve(path);
ui->widget->xAxis->setLabel(xname);
ui->widget->yAxis->setLabel(yname);
}
void PostCurveManage::addCurve(QString name,QVector<double> x,QVector<double> y)
{
ui->widget->graph(0)->setName(name);// 设置图例名称
showCurve(x,y);
}
void PostCurveManage::addCurve(QString path,int x1, int x2, int y1, int y2)
{
addCurve(path);
ui->widget->xAxis->setRange(x1, x2);
ui->widget->yAxis->setRange(y1, y2);
}
void PostCurveManage::setCurveName(QString name)
{
ui->widget->graph(0)->setName(name);// 设置图例名称
}
void PostCurveManage::setShaftScope(int x1,int x2,int y1,int y2)
{
ui->widget->xAxis->setRange(x1, x2);
ui->widget->yAxis->setRange(y1, y2);
}
里面设置了很多方法供大家使用,后续不知道会不会增加了,如有增加的小伙伴,也可以在评论中写到。
下载源码地址:传送门