CSV to QTaleWidget
-
Hi, I am trying to read the csv file and showing the information through a dialog containing a QTableWidget, using this code:
QFile file("users.csv"); QTextStream in(&file); QString loadCsv; int rowsCount = 1; while(!in.atEnd()) { QString line = in.readLine(); loadCsv = line.split("\t")[rowsCount - 1]; int pos = loadCsv.lastIndexOf(','); loadCsv = loadCsv.left(pos); ui->tableWidget->setRowCount(rowsCount); ui->tableWidget->setColumnCount(loadCsv.size()); for(int col = 0; col < loadCsv.size(); ++col) { QTableWidgetItem *items= new QTableWidgetItem(loadCsv.at(col)); ui->tableWidget->setItem((rowsCount - 1), col, items); } rowsCount++; }
but it just shows me a blank table. What am I doing wrong?
-
@Davide00
Unless you put someqDebug()
statements in there, we/you do not know how many rows or columns it is encountering or what visible string you are putting in any cells....ui->tableWidget->setColumnCount(loadCsv.size());
If, for whatever reason, a blank line is encountered, say at the end, this would remove all previously-added columns from the table. Do you think this is safe coding?
-
@Davide00 said in CSV to QTaleWidget:
QString line = in.readLine(); loadCsv = line.split("\t")[rowsCount - 1]; int pos = loadCsv.lastIndexOf(','); loadCsv = loadCsv.left(pos);
This looks strange. You're parsing a single line here, I'm not sure why you are using
rowsCount
here. Is your data tab delimited or comma delimited? I would expect this to be just:QString delimiter = ","; //specify the delimiter of your file QStringList loadCsv = line.split(delimiter);
-
@mchinand my data are delimited using "\t".
Actually this is a code i found on the internet, i tried to modify it because, ad the end of every row i used ",\n" to start a new line.
For example
"name1" << "\t" << "email1" << ",\n"
"name2" << "\t" << "email2" << ",\n" etc.
The problem is, it worked but it showed me ' , ' at the end of every row. I tried to modify the code in order to cancel the ' , ' but now it just show me a blank table. -
@JonB I've modified my code and used, now it looks like this:
void table_dialog::fill_table() { QFile file("utenti.csv"); if(!file.open(QFile::ReadOnly | QFile::Text)) { return; } QTextStream in(&file); int cter = 0; while(in.atEnd()) { QString readedLine = in.readLine(); QStringList listValue = readedLine.split(","); cter += 1; for(int i = 0; i < listValue.size(); ++i) { ui->tableWidget->setItem(cter-1, i, new QTableWidgetItem(listValue[i])); } } file.flush(); file.close(); }
I used some qDebug too, and i noticed that readLine() and .split(",") are doing nothing (now i changed the delimeter from "\t" to ",").
What could possibly be the problem? -
@Davide00 said in CSV to QTaleWidget:
I used some qDebug too, and i noticed that readLine() and .split(",") are doing nothing (now i changed the delimeter from "\t" to ",").
Is your
while
loop not executed for each line in your file anymore? Are you setting the row and column count of your table anywhere? What do you meanreadLine()
andsplit()
do nothing? What is the output of each? If you addqDebug()
statements, include them in the code you paste here and what does the debugging output show. Also, can you also show us the first few rows of your csv file? -
@mchinand the code is the same as before, i just corrected the while condition into while(!in.atEnd())
I added a qDebug() like thisQString readedLine = in.readLine(); qDebug() << readedLine; QStringList listValue = readedLine.split(","); qDebug() << listValue
and i obtain the following:
20:24:10: Starting /home/LABORATORI/dc858025/login_form/login_form/login_form ... "Wonder,Woman,3562145969,abcd,mer dic 25 1963,donna" ("Wonder", "Woman", "3562145969", "abcd", "mer dic 25 1963", "donna") "Bruce,Wayne,imbatman@gmail.com,TheJoker27,mar mag 6 1975,uomo" ("Bruce", "Wayne", "imbatman@gmail.com", "TheJoker27", "mar mag 6 1975", "uomo") 20:24:24: /home/LABORATORI/dc858025/login_form/login_form/login_form exited with code 0
which is right considering that the csv file is exactly mae up of this two lines
Wonder | Woman | 3562145969 | abcd | mer dic 25 1963 | donna Bruce | Wayne | imbatman@gmail.com | TheJoker27 | mar mag 6 1975 | uomo
the problem is that the QTableWidget is still empty, and i don't know why
-
-
@Christian-Ehrlicher I've followed an half semester course on c++ and only the last three lessons were on Qt, so I basically know nothing.
How can i set a row and column? -
@Christian-Ehrlicher said in CSV to QTaleWidget:
I've never used QTableWidget, but I thought rows and columns were created automatically as GridLayout do.
Disappointed i am :) -
@Davide00 said in CSV to QTaleWidget:
How can i set a row and column?
By clicking on my links and reading the documentation.
-
So something like this
void table_dialog::fill_table() { QFile file("utenti.csv"); if(!file.open(QFile::ReadOnly | QFile::Text)) { return; } QTextStream in(&file); int cter = 0; QString readedLine = in.readLine(); QStringList listValue = readedLine.split(","); ui->tableWidget->setColumnCount(listValue().size()); while(!in.atEnd()) { QString readedLine = in.readLine(); QStringList listValue = readedLine.split(","); cter += 1; ui->tableWidget->setRowCount(cter); for(int i = 0; i < listValue.size(); ++i) { ui->tableWidget->setItem(cter-1, i, new QTableWidgetItem(listValue[i])); } } file.flush(); file.close(); }
should do the work?
-
You're reading the first line of your file in your three lines before the
while
loop. You could add a header to your csv file, so the header is read first and then all your data rows are read within the while loop. Alternatively, remove thatreadline()
before thewhile
loop and add this within loop:if (listValue.size() > ui->tableWidget->columnCount()){ ui->tableWidget->setColumnCount(listValue.size()); }