Qt 读取csv文件并且绘制折线图

直接上代码:

 1 void MainWindow::readcsvfile() //读取csv
 2 {
 3  QFile csvFile("C:/Users/Administrator/Desktop/Demo/0702.CSV");
 4  QStringList csvList;
 5  csvList.clear();
 6  if (csvFile.open(QIODevice::ReadWrite)) //对csv文件进行读写操作
 7  {
 8   QTextStream stream(&csvFile);
 9   while (!stream.atEnd())
10  {
11  csvList.push_back(stream.readLine()); //保存到List当中
12  }
13  csvFile.close();
14  }
15  else
16  {
17      QMessageBox::about(NULL, "csv文件", "未打开该文件!");
18  }
19    int i = 0;
20    Q_FOREACH(QString str, csvList)   //遍历List
21   {
22    i = i + 1;
23    QStringList valsplit = str.split(","); //分隔字符串
24    if(i > 2)
25    {
26     //得到深度、声速、温度
27     QString depth = valsplit[0];
28     QString sonicvelocity = valsplit[1];
29     QString temperature = valsplit[2];
30     double depthvalue = depth.toDouble();
31     double sonicvalue = sonicvelocity.toDouble();
32     double tempvalue = temperature.toDouble();
33     //Q//MessageBox::warning(NULL, "dd", QString::number(tempvalue));
34     QPointF point;
35     point.setX(depthvalue);
36     point.setY(sonicvalue);
37     QPointF point2;
38     point2.setX(depthvalue);
39     point2.setY(tempvalue + 1510);
40     vectors.append(point);
41     vector2.append(point2);
42    }
43   }
44 }
45  
46 void MainWindow::lineChart() //绘制图
47 {
48     //设置X,Y标题
49     ui->qwtPlot->setAxisTitle(QwtPlot::xBottom, QString::fromLocal8Bit("深度(m)"));
50     ui->qwtPlot->setAxisTitle(QwtPlot::yLeft, QString::fromLocal8Bit("声速(m/s)"));
51     ui->qwtPlot->setAxisTitle(QwtPlot::yRight, QString::fromLocal8Bit("温度(°C)"));
52     ui->qwtPlot->enableAxis(QwtPlot::yRight,true);
53     ui->qwtPlot->setAxisScale(QwtPlot::yLeft,1538,1540,0.2);
54     ui->qwtPlot->setAxisScale(QwtPlot::xBottom,0,30,2);
55     ui->qwtPlot->setAxisScale(QwtPlot::yRight,28,30,0.2);
56  
57     //ui->qwtPlot->set
58     //构造曲线数据
59      QwtPointSeriesData* series = new QwtPointSeriesData(vectors);
60      //设置网格
61      QwtPlotGrid* grid = new QwtPlotGrid();
62      grid->setPen(QColor(222, 222, 222), 1);
63      grid->attach(ui->qwtPlot);
64      //create plot item
65      QwtPlotCurve* curve1 = new QwtPlotCurve(QString::fromLocal8Bit("声速"));
66      //设置数据
67      curve1->setData(series);
68      //设置画笔颜色==就是图像颜色
69      curve1->setPen(QColor(255, 0, 0), 2, Qt::SolidLine);
70      //使曲线更光滑
71      curve1->setCurveAttribute(QwtPlotCurve::Fitted, true);
72      //把曲线附加到qwtPlot上
73      curve1->attach(ui->qwtPlot);
74      //添加温度-深度曲线
75      //构造曲线数据
76      QwtPointSeriesData* series2 = new QwtPointSeriesData(vector2);
77      //create plot item
78      QwtPlotCurve* curve2 = new QwtPlotCurve(QString::fromLocal8Bit("温度"));
79      //设置数据
80      curve2->setData(series2);
81      //设置画笔颜色=图像颜色
82      curve2->setPen(QColor(127, 222, 335), 2, Qt::SolidLine);
83      //使曲线更光滑
84      curve2->setCurveAttribute(QwtPlotCurve::Fitted, true);
85      //把曲线附加到qwtPlot上
86      curve2->attach(ui->qwtPlot);
87  
88      //设置图例
89      QwtLegend *legend = new QwtLegend;
90      legend->setDefaultItemMode(QwtLegendData::ReadOnly);
91      ui->qwtPlot->insertLegend(legend,QwtPlot::BottomLegend);//插入图例
92      ui->qwtPlot->replot();
93      ui->qwtPlot->show();
94 }

需要第三方库QWT

运行结果:

 

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QT嵌入式开发,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值