Item in QTableWidget
-
@for (int row=0 ; row < ui->widget->rowCount();row++)
{
qDebug()<<"Ckeck point X "<<row;
QTableWidgetItem *item_test = ui->widget->item(row, 0);
if(!item_test->text().isEmpty())
{
check_count++;
qDebug()<<"Checkpoint y "<<row;
}
c++;
}
qDebug()<<"Check Count"<<check_count;@for first loop(row=0) its working but in the next loop(row=1) it displays Ckeck point X 1;then the program is terminated
pls help me to fix this problem
-
Take a look at these lines:
@
QTableWidgetItem *item_test = ui->widget->item(row, 0);
if(!item_test->text().isEmpty())
@What happens if line 1 above sets item_test to 0? In line 2 you dereference the pointer, but you have no idea if the pointer is valid or not. So, do this instead:
@
QTableWidgetItem *item_test = ui->widget->item(row, 0);
if(item_test) {
if(!item_test->text().isEmpty()) {
@Though you could combine that to:
@
QTableWidgetItem *item_test = ui->widget->item(row, 0);
if(item_test && !item_test->text().isEmpty())
@Dereferencing a 0 (or an otherwise invalid) pointer will lead you into "undefined behaviour", usually leading to a crash or worse. Using either *the_pointer. or the_pointer-> is called dereferencing the pointer. That is: not trying to use the pointer as such, but the thing it points to. If the thing it points to is invalid, that will go wrong.