Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何从文本文件中读取数据并将其存储在QVector中,以便使用QCustomPlot进行打印?_Qt_Vector_Plot_Readfile - Fatal编程技术网

Qt 如何从文本文件中读取数据并将其存储在QVector中,以便使用QCustomPlot进行打印?

Qt 如何从文本文件中读取数据并将其存储在QVector中,以便使用QCustomPlot进行打印?,qt,vector,plot,readfile,Qt,Vector,Plot,Readfile,出于学习目的,我想在Qt中使用QCustomPlot绘制存储在文本文件中的数据。QCustomPlot接受一个QVector(double),当我手工创建向量(使用值初始化向量)时,绘图工作正常。但是当我从文本文件中读入值时,什么也没有发生。我没有编译器错误,也没有绘图 我使用了中的代码片段。您可以在下面看到我正在使用的代码。我花了几个小时在网上阅读,但没有用 QVector<double> v; QVector<double> x(20), y(20);

出于学习目的,我想在Qt中使用QCustomPlot绘制存储在文本文件中的数据。QCustomPlot接受一个QVector(double),当我手工创建向量(使用值初始化向量)时,绘图工作正常。但是当我从文本文件中读入值时,什么也没有发生。我没有编译器错误,也没有绘图

我使用了中的代码片段。您可以在下面看到我正在使用的代码。我花了几个小时在网上阅读,但没有用

    QVector<double> v;
    QVector<double> x(20), y(20);
    QFile textFile("Data3.txt");
    if(textFile.open(QIODevice::ReadOnly))
    {
        double d;
        QTextStream textStream(&textFile);
        while (!textStream.atEnd()) {
            textStream >> d;
            if(textStream.status() == QTextStream::Ok){
                v.append(d);
            }
            else
                break;
        }
        for(int i=0; i<=20; ++i)
        {
            x[i] = i;
            y[i] = v[i];
        }

}

    QVector<double>   myVectorx({0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19});
    QVector<double> myVectory({0.5,1,3,5,7,10});
    ui->custom2Widget->addGraph();

    ui->custom2Widget->graph()->setData(myVectorx, y);
    //ui->custom2Widget->graph()->addData(x,y);
    ui->custom2Widget->graph()->setPen(QPen(Qt::blue));
    ui->custom2Widget->xAxis->setRange(0,25);
    ui->custom2Widget->yAxis->setRange(0, 100);
    ui->custom2Widget->xAxis->setLabel("Time");
    ui->custom2Widget->yAxis->setLabel("CO2 values");
    ui->custom2Widget->replot();
qv;
qx(20),y(20);
QFile文本文件(“Data3.txt”);
if(textFile.open(QIODevice::ReadOnly))
{
双d;
QTextStream textStream(&textFile);
而(!textStream.atEnd()){
textStream>>d;
if(textStream.status()==QTextStream::Ok){
v、 附加(d);
}
其他的
打破
}
对于(inti=0;icustom2Widget->addGraph();
ui->custom2Widget->graph()->setData(myVectorx,y);
//ui->custom2Widget->graph()->addData(x,y);
ui->custom2Widget->graph()->setPen(QPen(Qt::blue));
ui->custom2Widget->xAxis->setRange(0,25);
ui->custom2Widget->yAxis->setRange(01100);
ui->custom2Widget->xAxis->setLabel(“时间”);
ui->custom2Widget->yAxis->setLabel(“CO2值”);
ui->custom2Widget->replot();
数据文件如下所示(它是一列,而不是在同一行中一列接一列):

315.71 317.45 317.5 317.1 315.86 314.93 313.2 312.66
313.33

在提供的示例中,setRanges为false。它们对于数据来说太低。在我的应用程序中,我有更宽的x轴和y轴值。首先,在填充
x
y
向量的
for
循环中,您超出了它们的范围。这些可以有20个元素,因此您可以访问
[0;20]
索引,同时循环转到