How to Read csv data in tablewidget ?
-
I am very new to Qt and learning to use it . For an easy practice i thought to write a simple program which will will read the data from a csv file to QTablewideget. I have wrote a sample code to get started but unfortunately i can't set the column/row correctly and read the data line by line to the table.
Here is the code:
@#include <QApplication> #include <QMainWindow> #include <QTableWidget> #include <QTableWidgetItem> #include <QFile> #include <QString> #include <QStringList> #include <QMessageBox> #include <QTextStream> #include <QDebug> int main(int argc, char **argv) { QApplication app(argc, argv); QMainWindow *window = new QMainWindow(); QMessageBox msg; QFile file("Data.txt"); if(!file.open(QIODevice::ReadOnly)) msg.information(0,"Error!","Error opening file!",0); QTextStream in(&file); QStringList loadCsv; QTableWidget *myTable=new QTableWidget(); while(!file.atEnd()){ loadCsv<<in.readLine().split(";"); myTable->setColumnCount(loadCsv.size()); for(int row=0;row<in.readLine().size();++row){ myTable->setRowCount(loadCsv.size()); //It reading only first line. for(int col=0;col<loadCsv.size();++col){ myTable->setColumnCount(loadCsv.size()); QTableWidgetItem *Items= new QTableWidgetItem(loadCsv[col]); myTable->setItem(row,col,Items); } } } qDebug()<<loadCsv; window->setCentralWidget(myTable); window->show(); return app.exec(); }
@
In CSV Data.txt i have following data:**
bq. John Kl;34;1335532;CA;0444344
Kuma jo;54;44432;NY;0322355
Lebal ho;24;44022;NY;0110004Spent more than 2 days to figure it out myself but solution does not come to me since my logic is sucks. But i maybe i did
not able to work properly whith while/for loop. This code currently setting 5 columns and 5 row and reading first line then setting the same line to first two row. I have taken screenshot if this helps: !http://oi39.tinypic.com/2a0hyqp.jpg()!If someone can help me with an example code(Or modification of this code) then it would be a great lession for me to study about.
Please help me!
Thanks
-
welcome to devnet
A bit of proper indentation in your source would help to read it better.
@
QTextStream in(&file);
QStringList loadCsv;QTableWidget *myTable=new QTableWidget(); while(!file.atEnd()){ loadCsv<<in.readLine().split(";"); myTable->setColumnCount(loadCsv.size()); for(int row=0;row<in.readLine().size();++row){ myTable->setRowCount(loadCsv.size()); //It reading only first line. for(int col=0;col<loadCsv.size();++col){ myTable->setColumnCount(loadCsv.size()); QTableWidgetItem *Items= new QTableWidgetItem(loadCsv[col]); myTable->setItem(row,col,Items); } } }
@
The problem seem to be in line 11. You read a line and fill always the whole starting at the first row again.
Note: that is just a quick guess. I did not study the code in detail.
-
The solution is to re-write the cycle in the following way:
@int rowsCount = 1;
while(!in.atEnd()) { loadCsv = new QStringList(in.readLine().split(";")); myTable->setRowCount(rowsCount); myTable->setColumnCount(loadCsv->size()); for(int col = 0; col < loadCsv->size(); ++col) { QTableWidgetItem *items= new QTableWidgetItem(loadCsv->at(col)); myTable->setItem((rowsCount - 1), col, items); } rowsCount++; delete loadCsv; }
@
-
Hi
and another exaple, CSV-> QTableWidget@
void MainWindow::readFile(){
QFile file("file_csv.csv");
QStringList listA;
int row = 0;
if (file.open(QIODevice::ReadOnly)){
while (!file.atEnd()){
QString line = file.readLine();
listA = line.split(",");
ui->tableWidget->setColumnCount(listA.size());
ui->tableWidget->insertRow(row);
for (int x = 0; x < listA.size(); x++){
QTableWidgetItem *test = new QTableWidgetItem(listA.at(x));
ui->tableWidget->setItem(row, x, test);
}
row++;
}
}
file.close();
}
@