0

I have a QTableWidget called tw_topic. It's not empty. In another function I need the text of the items.

Code :

for(int i = ui->tw_topic->rowCount(); i >= 0; i--)
{
    //should return the first item of the first column
    const QString itm = ui->tw_topic->item(i, 0)->text();
    //Here I will do some other stuff...
}

Somehow it crashes at the point where itm gets initialized and I dont know why.

  • 1
    one of `ui, tw_topic` or what is returned by `item(i, 0)` is `NULL` for some reason. Try breaking down the statement in to single steps. You will find out which one is NULL. Or debug and try stepping in to the function. – stardust May 07 '13 at 17:28

1 Answers1

1

I found out that the for loop was the problem.

It should look like this :

for(int i = 0; i < ui->tw_topic->rowCount(); i++)
{ 
    // stuff 
}

If i is ui->tw_topic last row it'll crash.

  • 3
    Or, if for anything, you want to keep the backwards (for example, deleting items is easier backwards, so you dont have to mess with index changes ) `for(int i = ui->tw_topic->rowCount() - 1; i >= 0; i--)` – trompa May 07 '13 at 20:20