c++ Qt QTableWidgetItem导致崩溃

C++ Qt QTableWidgetItem causes crash

本文关键字:崩溃 QTableWidgetItem Qt c++      更新时间:2023-10-16

我有一个名为tw_topic的QTableWidget。它不是空的。在另一个函数中,我需要条目的文本。

代码:

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

在itm初始化的时候崩溃了,我不知道为什么。

我发现是for循环出了问题。

应该是这样的:

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

如果iui->tw_topic的最后一行,它将崩溃。