PDA

View Full Version : To store the Qtablewidget data to .csv or xl file - Issue



patilakash
28th April 2016, 14:43
The following is my piece of code:


QString fileName = appData + DIR+ "AISTABLE";
fileName = fileName + d_local.toString("_yyyy-MM-dd") + t_local.toString("_HH-mm-ss") + ".csv";
f_out = fopen(fileName.toAscii().data(), "wb+");
if (f_out == NULL) {
return;
}

QString headerString = "d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11\r\n";

for(int row = 0;row <ui->tableWidget->rowCount()-1; row++)
{
for(int column = 0;column< ui->tableWidget->columnCount()-1 ;column++)
{
headerString += ui->tableWidget->item(row,column)->text() +",";
}
headerString += "\r\n";
}
fprintf(f_out,"%s",headerString.toAscii().data());
fflush(f_out);
fclose(f_out);

I have created the Qtablewidget using Qt designer with rows = 300 and column = 12.
When I execute the above program, I get the following error:

Unhandled exception at 0x5463d72c in File.exe: 0xC0000005: Access violation reading location 0x00000000.

What is the problem for not getting output?

anda_skoa
28th April 2016, 17:44
You program crashes, which has nothing to do with "getting output".

My guess would be that you only have a sparsely populated table and not all item() calls return a valid pointer.

Cheers,
_

d_stranz
28th April 2016, 22:07
Your problem is likely what anda_skoa mentions. But in addition, you are opening the file in binary, not text mode, so what do you expect to get using fprintf? And your code will also stop copying before it reaches the last column of each row as well as the last row of the table. So you need to do a little bit of error checking on the returned QTableWidgetItem pointers as well as your iteration logic.