I have two different projects where I use QTableWidget
object and I have the same issue in the both.
I try to fill the QTableWidget
object with new QTableWidgetItem
but some of items are missed, they are not represented in the table.
The problem appears with MinGW and MSVC compilers.
I've tried it with 5.12.1 and 5.12.5 Qt versions.
Please help me to figure what is wrong with it out.
I use very simple code like below
In one project if I call createItems()
twice in a row so the table is filled
but the other project uses greater table so this code works if I call it much more times, but not fully anyway
void MainWindow::createItems()
{
for (int r = 0; r < ui->tableWidget->rowCount(); ++r)
{
for (int c = 0; c < ui->tableWidget->columnCount(); ++c)
{
QTableWidgetItem *pi = ui->tableWidget->item(r, c);
if (pi != nullptr) continue;
pi = new QTableWidgetItem();
ui->tableWidget->setItem(r, c, pi);
qDebug() << r << c << pi << ui->tableWidget->item(r, c)
<< (pi == ui->tableWidget->item(r, c));
}
}
}
it's a part of qDebug output
0 0 0x2c4af2f0 0x2c4af2f0 true
0 1 0x2c4af970 0x2c4af970 true
0 2 0x2c4af9b0 0x2c4af9b0 true
0 3 0x2c4afa70 0x2c4afa70 true
0 4 0x2c4aeb30 0x2c4aeb30 true
0 5 0x2c4aecb0 0x2c4aecb0 true
1 0 0x2c4aed30 0x2c4aed30 true
1 1 0x2c4aedf0 0x2c4aedf0 true
1 2 0x2c4aee30 0x2c4af9b0 false
2 0 0x2c4afdf0 0x2c4afdf0 true
2 1 0x2c4aeef0 0x2c4aeef0 true
2 2 0x2c4aef30 0x2c4af9b0 false
please pay attention to (1, 2) and (0, 2) and (2, 2), why it happens?
and then I fill items in a cycle like below. Code is crashed when not all of the items are created, it's obvious that item pointer is null but I'm sure that it's been created
for (int r = 0; r < rows_amount; ++r)
{
ui->tableWidget->item(r, 0)->setData(Qt::DisplayRole, r);
ui->tableWidget->item(r, 1)->setData(Qt::DisplayRole, r+1);
}
in the second project I fill items when they are created by 'new' command, then I call something like pTable->setItem(r, c, pItem)
In the second project where setItem
is used, the table looks like this (my real example)
It works normally on some computers, and the code is simple so I don't have any idea what I'm doing wrongly