I normaly read a csv file with <fstream>
lib and save that info in a vector<MyStruct>
lib but on this kind of project we have to use something call <QFile>
and <QTextStream>
and I don't really know how to use it, even that freaky vector QStringList
-what a headache-.
I want to render a csv into a widget table and save all info in a vector. this is the struct
struct Person
{
QString name;
int age;
double salary;
};
and my csv structure is like:
name,age,salary
name,age,salary
name,age,salary
this is my code until now
persons[0].name = "Victor Ramos";
persons[0].age= 10;
persons[0].salary= 10000.0;
persons[1].name = "Ana Ramos";
persons[1].age= 15;
persons[1].salary= 12000.0;
int fila;
ui->tableWidget->insertRow(ui->tableWidget->rowCount());
fila = ui->tableWidget->rowCount() - 1;
ui->tableWidget->setItem(fila, NOMBRE, new QTableWidgetItem(nombre));
ui->tableWidget->setItem(fila, EDAD, new QTableWidgetItem(QString::number(edad)));
ui->tableWidget->setItem(fila, SALARIO, new QTableWidgetItem(QString::number(salario)));
Normaly I would do this if I were working with a simple console app
#include <fstream>
#include <vector>
int main(){
vector<Person> persons; Person p;
istream file('myFile.csv');
string line;
if(!file.is_open()){ return 1; }else{
while(!file.eof()){
getLine(file, line, ',');
p.name = line;
getLine(file, line, ',';
p.edad = stoi(line);
getLine(file, line);
//string to float recive a string a return a float
p.salary = stringToFloat(line);
persons.push_back(p);
}
}
file.close();
/*Show vector persons with a simple for*/
}
Thank you.