PDA

View Full Version : how to read null value from QTableWidget?



aurora
25th January 2012, 10:32
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?

stampede
25th January 2012, 11:16
But when i'm trying to read table widget, if it encounters null cell, program crashes...
Can you show some code relevant to this ?

aurora
25th January 2012, 11:20
Can you show some code relevant to this ?

Ya sure...


for(int r=0;r<rowCounter;r++)
{
QTableWidgetItem *rwPtr= new QTableWidgetItem();

rwPtr=SearchTable->item(r,j);
QString PrCell=rwPtr->text();

if(PrCell==Val)
{
int NewRowCount=TableOutput->rowCount();
TableOutput->insertRow(NewRowCount);
for(int m=0;m<ColCounter;m++)
{
QTableWidgetItem *cellPtr=new QTableWidgetItem();
cellPtr=SearchTable->item(r,m); <<<-- here program crashing, if null presents
std::cout<<cellPtr->text().toStdString()<<" ";
QTableWidgetItem *newCell=new QTableWidgetItem(cellPtr->text());
TableOutput->setItem(NewRowCount,m,newCell);
}
}
}

stampede
25th January 2012, 11:41
First of all, you are leaking memory:


QTableWidgetItem *rwPtr= new QTableWidgetItem(); // rwPtr holds new object now
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:
QTableWidgetItem *rwPtr = SearchTable->item(r,j);

You can't do that. I mean, you can, but you shouldn't.
Anyway, it crashes, because you never check if the pointer is valid before dereferencing:


cellPtr=SearchTable->item(r,m);
std::cout<<cellPtr->text().toStdString()<<" "; // what if cellPtr == NULL here ?

Fix the above problems and let us know if it still crashes.

aurora
25th January 2012, 12:04
ok thank u stampede....
I want put "NULL" string in those cells, where item is not present...
How can i do that?

stampede
25th January 2012, 12:14
I don't know if I understood you right, but try this:


QTableWidgetItem *cellPtr = SearchTable->item(r,m);
QTableWidgetItem *newCell=new QTableWidgetItem(cellPtr ? cellPtr->text() : "NULL"); // remember to check your pointers !
TableOutput->setItem(NewRowCount,m,newCell);

aurora
25th January 2012, 12:37
Thanks alot Sampede...
Thats what i wanted...