Im creating a program that reads from a text file and plots the integers. Here is what I have so far
#include "realtime.h"
#include "ui_realtime.h"
#include <QFile>
#include<QIODevice>
Realtime::Realtime(QWidget *parent) :
QWidget(parent),
ui(new Ui::Realtime)
{
ui->setupUi(this);
int size = 1000;
QVector<double> x(size), y(size);
for (int i=0; i<size; i++)
{
x[i]= i;
y[i]= i ;
}
ui->plot->addGraph();
ui->plot->graph(0)->setData(x,y);
ui->plot->xAxis->setRange(0,10);
ui->plot->yAxis->setRange(0,10);
}
Realtime::~Realtime()
{
delete ui;
}
int main()
{
std::vector<int>ints;
QFile file("2dplotarray.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
while (!file.atEnd())
{
QByteArray line = file.readLine();
QDataStream ds(line);
int int_in_line = 0;
ds >> int_in_line;
ints.push_back(int_in_line);
}
return 0 ;
}
Ignore the current x and y values, that was me testing the plotting features. How do i put the text file into the y values of my plot? The text file looks like this(NOT CODE just the best way to display it)
1
2
3
4
etc...
Unless the unstated point is to use the QT classes, I'd just use an input file stream if I were you. Here is what some code might look like to read in those values.
#include <fstream>
#include <string>
int main()
{
std::vector<int> vecValues;
std::ifstream file("yourfilename.txt");
if (file.is_open())
{
//if you can anticipate that the only values will be integers
//then this will work just fine
int value;
while (file >> value)
{
vecValues.push_back(value);
}
//or you could modify the following if you expected non-numbers
//in your file and need more control over handling them
//std::string fileLine;
//while ( std::getline(file, fileLine) )
//{
// vecValues.push_back(std::atoi(fileLine.c_str()));
//}
file.close();
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.