Load a csv file into a QTablewidget
-
Hi everyone,
I am working on a function that can load a csv into a QTableWidget.
Here is the QTableWidget where I would like to load my csv File.I have done a function that works very well to save a QTableWidget into a .csv file and here is the result:
I would now like to load it into my QTableWidget. I tested this code, but the data are not displayed.
QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), NULL, ("csv File(*.csv)")); QString data; QFile importedCSV(fileName); QStringList rowOfData; QStringList rowData; data.clear(); rowOfData.clear(); rowData.clear(); if (importedCSV.open(QFile::ReadOnly)) { data = importedCSV.readAll(); rowOfData = data.split("\n"); //Value on each row importedCSV.close(); } for (int x = 0; x < rowOfData.size(); x++) //rowOfData.size() gives the number of row { rowData = rowOfData.at(x).split(";"); //Number of collumn int r=rowData.size(); for (int y = 0; y < rowData.size(); y++) { ui->tableWidget_2->setItem(x,y,new QTableWidgetItem(rowData[y])); } } statusBar()->showMessage(tr("File successfully loaded."), 3000);
Thanks in advance for your help !
-
Hi everyone,
I am working on a function that can load a csv into a QTableWidget.
Here is the QTableWidget where I would like to load my csv File.I have done a function that works very well to save a QTableWidget into a .csv file and here is the result:
I would now like to load it into my QTableWidget. I tested this code, but the data are not displayed.
QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), NULL, ("csv File(*.csv)")); QString data; QFile importedCSV(fileName); QStringList rowOfData; QStringList rowData; data.clear(); rowOfData.clear(); rowData.clear(); if (importedCSV.open(QFile::ReadOnly)) { data = importedCSV.readAll(); rowOfData = data.split("\n"); //Value on each row importedCSV.close(); } for (int x = 0; x < rowOfData.size(); x++) //rowOfData.size() gives the number of row { rowData = rowOfData.at(x).split(";"); //Number of collumn int r=rowData.size(); for (int y = 0; y < rowData.size(); y++) { ui->tableWidget_2->setItem(x,y,new QTableWidgetItem(rowData[y])); } } statusBar()->showMessage(tr("File successfully loaded."), 3000);
Thanks in advance for your help !
@coilo I strongly suspect that your
tableWidget_2
has no rows and columns. You may check it by looking atrowCount()
andcolumnCount()
. I'm not sure but I don't see any code that would set size of the table.On the other hand - why you wouldn't create a custom model derived from
QAbstractTableModel
? Good example may be found here -
@coilo I strongly suspect that your
tableWidget_2
has no rows and columns. You may check it by looking atrowCount()
andcolumnCount()
. I'm not sure but I don't see any code that would set size of the table.On the other hand - why you wouldn't create a custom model derived from
QAbstractTableModel
? Good example may be found here@StarterKit Yes my tableWidget_2 has no row but it has 8 columns
Why should i create a QAbstractTableModel ? I am sorry i am new with qt so i don't really get it
-
@StarterKit Yes my tableWidget_2 has no row but it has 8 columns
Why should i create a QAbstractTableModel ? I am sorry i am new with qt so i don't really get it
@coilo Set proper row count using https://doc.qt.io/qt-5/qtablewidget.html#setRowCount before adding items
-
@coilo Set proper row count using https://doc.qt.io/qt-5/qtablewidget.html#setRowCount before adding items
@jsulm Oh nice it worked well ! I have a little problem, the QTableWidget display well the data, but there are " " between the data as you can see
However my .csv file does not contain any " "
Do you know where the problem could be coming from?
Btw here is the code which work :
QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), "/home", ("csv File(*.csv)")); QFile file(fileName); QString data; QStringList rowOfData; QStringList rowData; data.clear(); rowOfData.clear(); rowData.clear(); if (file.open(QFile::ReadOnly)) { data = file.readAll(); rowOfData = data.split("\n"); //Value on each row file.close(); } for (int x = 0; x < rowOfData.size(); x++) //rowOfData.size() gives the number of row { rowData = rowOfData.at(x).split(";"); //Number of collumn int r=rowOfData.size()-2; //idk why but there is a lag of two qDebug()<<r; for (int y = 0; y < rowData.size(); y++) { ui->tableWidget_2->setRowCount(r); ui->tableWidget_2->setItem(x-1,y,new QTableWidgetItem(rowData[y])); } }
-
@jsulm Oh nice it worked well ! I have a little problem, the QTableWidget display well the data, but there are " " between the data as you can see
However my .csv file does not contain any " "
Do you know where the problem could be coming from?
Btw here is the code which work :
QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), "/home", ("csv File(*.csv)")); QFile file(fileName); QString data; QStringList rowOfData; QStringList rowData; data.clear(); rowOfData.clear(); rowData.clear(); if (file.open(QFile::ReadOnly)) { data = file.readAll(); rowOfData = data.split("\n"); //Value on each row file.close(); } for (int x = 0; x < rowOfData.size(); x++) //rowOfData.size() gives the number of row { rowData = rowOfData.at(x).split(";"); //Number of collumn int r=rowOfData.size()-2; //idk why but there is a lag of two qDebug()<<r; for (int y = 0; y < rowData.size(); y++) { ui->tableWidget_2->setRowCount(r); ui->tableWidget_2->setItem(x-1,y,new QTableWidgetItem(rowData[y])); } }
@coilo said in Load a csv file into a QTablewidget:
ui->tableWidget_2->setRowCount(r);
Why do you call this in second loop?! You only need to do it once before the first loop.
Try:ui->tableWidget_2->setItem(x-1,y,new QTableWidgetItem(rowData[y].trimmed()));
-
@coilo said in Load a csv file into a QTablewidget:
ui->tableWidget_2->setRowCount(r);
Why do you call this in second loop?! You only need to do it once before the first loop.
Try:ui->tableWidget_2->setItem(x-1,y,new QTableWidgetItem(rowData[y].trimmed()));
-
@StarterKit Yes my tableWidget_2 has no row but it has 8 columns
Why should i create a QAbstractTableModel ? I am sorry i am new with qt so i don't really get it
@coilo said in Load a csv file into a QTablewidget:
Why should i create a QAbstractTableModel ? I am sorry i am new with qt so i don't really get it
I didn't propose to create
QAbstractTableModel
, I proposed to create your own model that will do CSV file operations and derive this new model class fromQAbstractTableModel
.
Have you checked the URL I provided? As you are new to Qt I highly recommend you to read it in order to better understand Qt approach for similar tasks. It will save you time in future. Of course you may useQTableWidget
alone if your task is pretty simple.