In my QTableWidget, there exist some cells whose values are Null(it contains nothing)
But when i'm trying to read table widget, if it encounters null cell, program crashes...
How can i get rid of this problem?
In my QTableWidget, there exist some cells whose values are Null(it contains nothing)
But when i'm trying to read table widget, if it encounters null cell, program crashes...
How can i get rid of this problem?
Can you show some code relevant to this ?But when i'm trying to read table widget, if it encounters null cell, program crashes...
Ya sure...
Qt Code:
for(int r=0;r<rowCounter;r++) { rwPtr=SearchTable->item(r,j); if(PrCell==Val) { int NewRowCount=TableOutput->rowCount(); TableOutput->insertRow(NewRowCount); for(int m=0;m<ColCounter;m++) { cellPtr=SearchTable->item(r,m); <<<-- here program crashing, if null presents std::cout<<cellPtr->text().toStdString()<<" "; TableOutput->setItem(NewRowCount,m,newCell); } } }To copy to clipboard, switch view to plain text mode
First of all, you are leaking memory:
You can't do that. I mean, you can, but you shouldn't.Qt Code:
rwPtr=SearchTable->item(r,j); // rwPtr value is overwritten, address of the object allocated above is lost and its impossible to release the memory now // this is how it should look:To copy to clipboard, switch view to plain text mode
Anyway, it crashes, because you never check if the pointer is valid before dereferencing:
Fix the above problems and let us know if it still crashes.Qt Code:
cellPtr=SearchTable->item(r,m); std::cout<<cellPtr->text().toStdString()<<" "; // what if cellPtr == NULL here ?To copy to clipboard, switch view to plain text mode
ok thank u stampede....
I want put "NULL" string in those cells, where item is not present...
How can i do that?
I don't know if I understood you right, but try this:
Qt Code:
QTableWidgetItem *newCell=new QTableWidgetItem(cellPtr ? cellPtr->text() : "NULL"); // remember to check your pointers ! TableOutput->setItem(NewRowCount,m,newCell);To copy to clipboard, switch view to plain text mode
aurora (25th January 2012)
Thanks alot Sampede...
Thats what i wanted...
Bookmarks