Reputation: 3203
In qt, I use tablewidget to store 100 rows. At first, I new tableWidgetItems to fill the rows. As it runs, I set the items' propertities and no longer 'new'. But I find after I use 'ui->tableWidget->setRowCount(index);', and later set back to 100, the code "ui->tableWidget->item(index, 0)->setText(...);" will crash the program. That's so bad!!! ;( I debugged and find the new index > index set as row count before 'setting back to 100'.
Did the system delete the table items automatically when I set smaller row count???
I fear about this so much because even my code cannot determine the lifetime of the objects I created... Does anyone know how to keep them 'alive' after setting row count?(otherwise, I have to new them...). I really appreciate it you take the patience to read my poor ELis:)
new:
//TABLE
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setRowCount(100);
ui->tableWidget->setHorizontalHeaderLabels(headers);
for(int i = 0; i < 100; i++)//new
{
ui->tableWidget->setItem( i, 0 , new QTableWidgetItem(""));//time
ui->tableWidget->setItem( i, 1 , new QTableWidgetItem(""));//name
ui->tableWidget->setItem( i, 2 , new QTableWidgetItem(""));//BITS
}
Related code lines only:
{
int index = 0;
for(int queue_i = size_1; queue_i >= 0; queue_i--)
{
if(logDisplayQueue.at(queue_i).at(3) == "0" || logDisplayQueue.at(queue_i).at(3) == "2")continue;
QStringList BITList = bits2Hexs(queue_i);
ui->tableWidget->item(index, 0)->setText(logDisplayQueue.at(queue_i).at(0));//time
ui->tableWidget->item(index, 1)->setText(logDisplayQueue.at(queue_i).at(1));//name
ui->tableWidget->item(index, 2)->setText(BITList.join(""));//BITS
if(queue_i == oldRowItemNo)ui->tableWidget->selectRow(index);
index++;
}
ui->tableWidget->setRowCount(index);//set row count to be 30 more or less
}
Another function:
{
ui->tableWidget->setRowCount(100);//back to be 100 again
for(int queue_i = size_1, index = 0; queue_i >= 0; queue_i--, index++)
{
QStringList BITList = bits2Hexs(queue_i);
ui->tableWidget->item(index, 0)->setText(logDisplayQueue.at(queue_i).at(0));//time
ui->tableWidget->item(index, 1)->setText(logDisplayQueue.at(queue_i).at(1));//name
ui->tableWidget->item(index, 2)->setText(BITList.join(""));//BITS
//In debugging, when index reches the value of old row-count, "->setText" crashes the //program.
if(queue_i == oldRowItemNo)ui->tableWidget->selectRow(index);
}
}
When running, it returns message like 'instruction 0x00421727 refers to 0x00000000 memory, the memory cannot be 'read''
if I comment off this line: 'ui->tableWidget->setRowCount(index);//set row count to be 30 more or less', it runs well without crash and rows after index-referred-row show the same data as before.
Upvotes: 0
Views: 138
Reputation: 206659
setRowCount
ensures that the table holds exactly that many rows. If you had more rows than index
before, those rows are gone (deleted).
If you want to temporarily hide rows, you should probably use hideRow(int)
/showRow
rather than resetting the row count.
Upvotes: 3