【问题标题】:QT customplot not correctQT customplot 不正确
【发布时间】:2017-07-13 08:22:07
【问题描述】:

我正在尝试从机翼的CSV 文件中绘制图表。我能够将值存储在QStringList 的数组中并绘制它。但是,在零值处有一个突然的跳跃。我在qDebug 中看到了输出,它显示了正确的值。请帮忙。我无法弄清楚问题所在。

这是我的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QMessageBox>
#include <QDebug>

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

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

static void process_line(const QByteArray &)
{
}

static void process_line(const QString &)
{
}

void MainWindow::on_read_clicked()
{
    QStringList fileData[1000];
    QStringList list2;
    QString line,line1;
    QString filename="C:/Users/PRASHANT/Desktop/xf-e197-il-200000.csv";

    QFile file(filename);
    if(!file.exists())
    {
        qDebug() << "NO File"<<filename;
    }
    else
    {
        qDebug() << filename<<"Success";
    }

    ui->textBrowser->clear();

    int i = 0;
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream stream(&file);
        QStringList list2;

        while (!stream.atEnd())
        {

            line = stream.readLine();
            d1 = line.indexOf("Alpha");

            list2 = line.split(',', QString::SkipEmptyParts);
            fileData[i]=list2;
            if(list2.contains("Alpha"))
            {
                start=i;
            }

            i++;
        }
    }

    file.close();


    // generate some data:
    QVector<double> x1(120), y1(120),y2(120),y3(120),y4(120); // initialize with entries 0..100

    for (int i=1; i<117; i++)
    {
      x1[i] = QString(fileData[start+i][0]).toDouble();
      y1[i] = QString(fileData[start+i][1]).toDouble();
      y2[i] = QString(fileData[start+i][2]).toDouble();
      y3[i] = QString(fileData[start+i][4]).toDouble();
      y4[i] = y1[i]/y2[i];
      qDebug() << x1[i] << y1[i];
    }

    // create graph and assign data to it:
    ui->Cl_apha->addGraph();
    ui->Cl_apha->graph(0)->setData(x1, y1);
    ui->CD_alpha->addGraph();
    ui->CD_alpha->graph(0)->setData(x1,y2);
    ui->CM_alpha->addGraph();
    ui->CM_alpha->graph(0)->setData(x1,y3);
    ui->CL_vs_CD->addGraph();
    ui->CL_vs_CD->graph(0)->setData(x1,y4);

    // give the axes some labels:
    ui->Cl_apha->xAxis->setLabel("\u03B1");
    ui->Cl_apha->yAxis->setLabel("CL");
    ui->CD_alpha->xAxis->setLabel("\u03B1");
    ui->CD_alpha->yAxis->setLabel("CD");
    ui->CM_alpha->xAxis->setLabel("\u03B1");
    ui->CM_alpha->yAxis->setLabel("CM");
    ui->CL_vs_CD->xAxis->setLabel("\u03B1");
    ui->CL_vs_CD->yAxis->setLabel("CL/CD");

    // set axes ranges, so we see all data:
    ui->Cl_apha->xAxis->setRange(-10, 15);
    ui->Cl_apha->yAxis->setRange(-1, 1.5);
    ui->CD_alpha->xAxis->setRange(-10, 15);
    ui->CD_alpha->yAxis->setRange(-0.05, 0.1);
    ui->CM_alpha->xAxis->setRange(-10, 15);
    ui->CM_alpha->yAxis->setRange(-0.2,0.05);
    ui->CL_vs_CD->xAxis->setRange(-10, 15);
    ui->CL_vs_CD->yAxis->setRange(-20, 85);
    ui->Cl_apha->replot();
    ui->CD_alpha->replot();
    ui->CM_alpha->replot();
    ui->CL_vs_CD->replot();
}

void MainWindow::on_write_clicked()
{
    QString fileName ="C:/Destination.txt";
    QFile file(fileName);
}

这是调试输出:

"C:/Users/PRASHANT/Desktop/xf-e197-il-200000.csv"  Success...
-10.75 -0.3896
-10.5 -0.4297
-10.25 -0.4338
-10 -0.4179
-9.75 -0.4017
-9.5 -0.405
-9.25 -0.4173
-9 -0.4406
-8.75 -0.4813
-8.5 -0.5118
-8.25 -0.5597
-8 -0.5294
-7.75 -0.5
-7.5 -0.4699
-7.25 -0.4375
-7 -0.4083
-6.75 -0.3814
-6.5 -0.3562
-6.25 -0.3319
-6 -0.3116
-5.75 -0.2891
-5.5 -0.2676
-5.25 -0.2467
-5 -0.2264
-4.75 -0.2038
-4.5 -0.181
-4.25 -0.1584
-4 -0.1349
-3.75 -0.11
-3.5 -0.0844
-3.25 -0.0586
-3 -0.0324
-2.75 -0.0067
-2.5 0.0197
-2.25 0.0458
-2 0.0721
-1.75 0.0989
-1.5 0.1245
-1.25 0.1508
-1 0.1775
-0.75 0.2026
-0.5 0.2282
-0.25 0.2542
0 0.2784
0.25 0.3054
0.5 0.3389
0.75 0.3777
1 0.4205
1.25 0.4694
1.5 0.5191
1.75 0.5702
2 0.5972
2.25 0.6155
2.5 0.6374
2.75 0.6609
3 0.6816
3.25 0.7056
3.5 0.7295
3.75 0.7518
4 0.7775
4.25 0.7995
4.5 0.8246
4.75 0.8482
5 0.8729
5.25 0.897
5.5 0.9219
5.75 0.9453
6 0.9716
6.25 0.9936
6.5 1.0172
6.75 1.0415
7 1.0643
7.25 1.0859
7.5 1.1067
7.75 1.1263
8 1.1452
8.25 1.1618
8.5 1.1748
8.75 1.1804
9 1.1795
9.25 1.1739
9.5 1.1679
9.75 1.1641
10 1.1629
10.25 1.1618
10.5 1.1609
10.75 1.159
11 1.1573
11.25 1.1548
11.5 1.1486
11.75 1.1425
12 1.1415
12.25 1.141
12.5 1.1366
12.75 1.1421
13 1.1466
13.25 1.1508
13.5 1.1543
13.75 1.1634
14 1.1718
14.25 1.18
14.5 1.1884
14.75 1.1949
15 1.1989
15.25 1.2009
15.5 1.1996
15.75 1.1953
16 1.1873
16.25 1.1764
16.5 1.1598
16.75 1.1414
17 1.1209
17.25 1.0971
17.5 1.0722
17.75 1.0455
18 1.0162

输出图 enter image description here

【问题讨论】:

标签: c++ qt qstring


【解决方案1】:

您的代码似乎没问题,但零值与文件中的 .toDouble() 函数有关,有些值无法从字符串转换为双精度值。
检查这个函数通过 refrence 和 if cluse 传递一个布尔变量,如下面的代码来查找丢失的数据。

bool ok = false;
double temp = QString(fileData[start+i][0]).toDouble(&ok);
if (ok == false)
{
    qDebug() << QString(fileData[start+i][0]);
}

【讨论】:

  • 尝试通过您生成的数据将数据设置为 qCustomPlot,就像一个简单的 qCustomPlot 演示。我确定您在将数据转换为双精度时出错。
猜你喜欢
  • 2018-12-23
  • 2016-10-09
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
相关资源
最近更新 更多