CSV to QTaleWidget
-
@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()); }