Search code examples
c++qtqtablewidgetqstringqtablewidgetitem

C++ Qt QTableWidgetItem causes crash


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.


Solution

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