Item in QTableWidget



  • how can i check the item in the Qtablewidget having value or not



  • What exactly do you mean by this?
    AFAIK there is a method to get a pointer to an item, then you can query the values of the item. Does that help you?



  • @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



  • Always check if a pointer you get back from a method call that returns one is valid before dereferencing it. You are using item_test without checking if it is valid in line 5.



  • sorry i didn't catch your point.
    Can u pls explain in detail.



  • 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.



  • Thank U ,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.