Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

(SOLVED) All items identical in QTableWidget



  • Hi all,

    I'm sure I'm doing something wrong but I can't see what it is.

    First I initialize a QTableWidget as follows:

    @ for (int row = 0; row < ui->AssetTable->rowCount(); ++row)
    for (int column = 0; column < ui->AssetTable->columnCount(); ++column)
    {
    QTableWidgetItem *newItem = new QTableWidgetItem(QString::number(row)+":"+QString::number(column));
    ui->AssetTable->setItem(row, column, newItem);
    }
    @

    When I try to access the items later with this code:

    @ for (int row = 0; row < ui->AssetTable->rowCount(); ++row)
    for (int column = 0; column < ui->AssetTable->columnCount(); ++column)
    {
    QTableWidgetItem *it = ui->AssetTable->item(row, column);
    QString x = it->text();
    }
    @

    all of the items retrieved are the same item. They all exhibit the value "0:0", and if I try to change several of them only the one at 0,0 changes. What am I doing wrong?



  • Hi.
    Please these two lines
    you didn't set rows and columns of table.
    @ui->AssetTable->setRowCount(5);
    ui->AssetTable->setColumnCount(5);@

    hope this would help.



  • Thanks for your answer.
    I did set the row and column count in the form. Do I have to do it in code as well?



  • Never mind, it's working now although I'm not sure what is different. It's too late to try to figure it out tonight.

    Thanks.



  • Hi.
    Please update your thread title as (SOLVED) at the begining.
    Thank you.


Log in to reply