【QT应用】Qt QCustomPlot 读取CSV文件,生成曲线

效果如图:

mainWindow.h 文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_btn_open_csvFile_clicked();

    void on_btn_get_csv_clicked();
    void outputCSV();

    void on_btn_geneate_csvFile_clicked();

    void on_btn_geneate_csvFile22_clicked();

    void on_btn_open_csvFile22_clicked();

    void on_btn_get_csv22_clicked();

private:
    Ui::MainWindow *ui;
    QString fileName;
    QString fileName22;
};
#endif // MAINWINDOW_H

mainWindow.cpp 文件

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString str = QStringLiteral("1你好世界 abc");	//推荐
    qDebug()<<str;

    this->fileName = "E:\\QT\\qcptest2\\cos.csv";
    this->fileName22 = "E:\\QT\\qcptest2\\sincos.csv";

    //曲线控件1
    //设置曲线名称1
    QCPTextElement *plotTitle = new QCPTextElement(ui->customplot);
    plotTitle->setText(QStringLiteral("单条曲线"));
    plotTitle->setFont(QFont("宋体", 16, QFont::Bold));
    ui->customplot->plotLayout()->insertRow(0);
    ui->customplot->plotLayout()->addElement(0, 0, plotTitle);

    ui->customplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

    ui->customplot->addGraph();
    ui->customplot->graph(0)->setName("value");

    ui->customplot->xAxis->setLabel(QStringLiteral("X轴"));
    ui->customplot->yAxis->setLabel(QStringLiteral("Y轴"));




    //曲线控件2
    //设置曲线名称1
    QCPTextElement *plotTitle2 = new QCPTextElement(ui->customplot2);
    plotTitle2->setText(QStringLiteral("两条曲线"));
    plotTitle2->setFont(QFont("宋体", 16, QFont::Bold));
    ui->customplot2->plotLayout()->insertRow(0);
    ui->customplot2->plotLayout()->addElement(0, 0, plotTitle2);

    ui->customplot2->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

    ui->customplot2->legend->setVisible(true);
    ui->customplot2->legend->setFont(QFont("Helvetica",9));

    // add two new graphs and set their look:
    ui->customplot2->addGraph();
    ui->customplot2->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
    ui->customplot2->graph(0)->setName("ps1");

    ui->customplot2->addGraph();
    ui->customplot2->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
    ui->customplot2->graph(1)->setName("ps2");


    ui->customplot2->xAxis->setLabel(QStringLiteral("X轴"));
    ui->customplot2->yAxis->setLabel(QStringLiteral("Y轴"));


}





MainWindow::~MainWindow()
{
    delete ui;
}


// 对于需要包含哪些头文件,写入TXT需要什么,写CSV也同样。
void MainWindow::outputCSV()
{
    QDateTime datetime = QDateTime::currentDateTime();
    QString timestr = datetime.toString("ddHHmmzzz");
//    this->fileName = "E:\\QT\\qcptest2\\cos.csv";
    QFile data(QString("E:\\QT\\qcptest2\\") + "timetable" + timestr + ".csv");            // 以上两行用时间戳作为文件名
    if(data.open(QFile::WriteOnly | QFile::Truncate))        // 打开文件
    {
        QTextStream out(&data);    // 输入流
        out << "name: ," << "jone," << "\n";
        out << "age: ," << "23," << "\n";
        out << " ," << " ," << "job: ," << "C++ development, " << "\n";
    }
    data.close();


}

void MainWindow::on_btn_geneate_csvFile_clicked()
{
    QDateTime datetime = QDateTime::currentDateTime();
    QString timestr = datetime.toString("ddHHmmzzz");

//    QFile data(QString("E:\\QT\\qcptest2\\") + "one" + timestr + ".csv");            // 以上两行用时间戳作为文件名
//    this->fileName = "E:\\QT\\qcptest2\\cos.csv";
    QFile data(this->fileName);
    if(data.open(QFile::WriteOnly | QFile::Truncate))        // 打开文件
    {
        QTextStream out(&data);    // 输入流
        double t = 0;
        double y1;
        double y2;
        double intv = 1;
        for (int i = 0; i < 100; ++i)
        {
            y1 = qSin(t);
            y2 = qCos(t);
            out <<QString::number(t)<<"," << QString::number(y1)<<"," << QString::number(y2)<<","<< "\n";

            t = t + intv;

        }

    }
    data.close();
}


void MainWindow::on_btn_geneate_csvFile22_clicked()
{
    QDateTime datetime = QDateTime::currentDateTime();
    QString timestr = datetime.toString("ddHHmmzzz");
//    QFile data(QString("E:\\QT\\qcptest2\\") + "two" + timestr + ".csv");            // 以上两行用时间戳作为文件名
//    this->fileName22 = "E:\\QT\\qcptest2\\sincos.csv";
    QFile data(this->fileName22);
    if(data.open(QFile::WriteOnly | QFile::Truncate))        // 打开文件
    {
        QTextStream out(&data);    // 输入流
        double t = 0;
        double y1;
        double y2;
        double intv = 1;
        for (int i = 0; i < 100; ++i)
        {
            y1 = qSin(t);
            y2 = qCos(t);
            out <<QString::number(t)<<"," << QString::number(y1)<<"," << QString::number(y2)<<","<< "\n";

            t = t + intv;
        }
    }
    data.close();

}


void MainWindow::on_btn_open_csvFile_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(
            this,tr("选择要打开的文件"),
            QDir::currentPath(),
            tr("文本文件(*.txt);;所有文件(*.*)"));
    if (!fileName.isEmpty())
    {
        this->fileName = fileName;
        qDebug()<<"filename:"<<fileName;
    }

}



void MainWindow::on_btn_open_csvFile22_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(
            this,tr("选择要打开的文件"),
            QDir::currentPath(),
            tr("文本文件(*.txt);;所有文件(*.*)"));
    if (!fileName.isEmpty())
    {
        this->fileName22 = fileName;
        qDebug()<<"filename:"<<fileName;
    }

}



void MainWindow::on_btn_get_csv_clicked()
{
    QVector<double> x, y;

    QStringList csvList;
    csvList.clear();


    //解析CSV文件

    QFile csvFile(this->fileName);

    if(csvFile.open(QIODevice::ReadWrite))
    {
        QTextStream stream(&csvFile);
        while(!stream.atEnd())
        {
            csvList.push_back(stream.readLine());
        }
        csvFile.close();
    }
    else
    {
        QMessageBox::about(nullptr,"csv 文件","打开失败");
    }

    int i = 0;
    Q_FOREACH(QString str,csvList)
    {
        i  = i + 1;
        QStringList valsplit = str.split(",");
        if(i>3)
        {
            x.push_back(valsplit[0].toDouble());
            y.push_back(valsplit[1].toDouble());
        }

    }


    ui->customplot->graph(0)->setData(x,y);

    //自适应x,y轴
    ui->customplot->rescaleAxes(true);

    ui->customplot->replot();


}





void MainWindow::on_btn_get_csv22_clicked()
{
    QVector<double> x, y,y2;

    QStringList csvList;
    csvList.clear();


    //解析CSV文件

    QFile csvFile(this->fileName22);

    if(csvFile.open(QIODevice::ReadWrite))
    {
        QTextStream stream(&csvFile);
        while(!stream.atEnd())
        {
            csvList.push_back(stream.readLine());
        }
        csvFile.close();
    }
    else
    {
        QMessageBox::about(nullptr,"csv 文件","打开失败");
    }

    int i = 0;
    Q_FOREACH(QString str,csvList)
    {
        i  = i + 1;
        QStringList valsplit = str.split(",");
        if(i>3)
        {
            x.push_back(valsplit[0].toDouble());
            y.push_back(valsplit[1].toDouble());
            y2.push_back(valsplit[2].toDouble());
        }

    }


    ui->customplot2->graph(0)->setData(x,y);
    ui->customplot2->graph(1)->setData(x,y2);

    //曲线自适应
    // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
    ui->customplot2->graph(0)->rescaleAxes();
    // same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
    ui->customplot2->graph(1)->rescaleAxes(true);

    ui->customplot2->replot();

}

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高亚奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值