2

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

Mikhail Z
  • 31
  • 6

1 Answers1

0

It seems that the Item pointer is not null! Can you check if it goes inside the if condition?

You should probably delete and null them is those cases.

Mahi
  • 332
  • 3
  • 11
  • Actually you are right and "(pi != nullptr)" condition is correct for some items, but it's strange to me anyway, the table is created from scratch and there shouldn't be any item. Also I tried to remove pointer checking and it means that all items should be replaced anyway, BUT I see it again "1 2 0x2c546b80 0x2c546540 false", some pointers has the same address. I see it at the same cells every time: (1, 2), (2, 2), (3, 2) and so on – Mikhail Z Oct 06 '19 at 18:08
  • I believe there's no guarantee in cpp (although it depends on compiler) but mostly you have to check every dynamic variable you created – Mahi Oct 16 '19 at 08:43