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

QTableWidget's. setText() does not set any text to cells



  • Hello,
    I use QTableWidgetItem to insert some text to cells in a table but all that happens is that it crashes with a segmentation error. Code below:

    QTableWidgetItem *item = new QTableWidgetItem;
    item = ui->tablewidget->item(2, 2);
    item->setText("danny");

    so when the last line executes it crashes with a segmentation fault that takes me to some at header file ?.

    any ideas?

    regards
    Frik Brits



  • You are not setting the item in the cell first. item is null.

    The fastest solution is to replace your 3 lines with:

    auto model = ui->tablewidget->model();
    model->setData(model->index(2,2),QStringLiteral("danny"));
    


  • Try,

    QTableWidgetItem *item = new QTableWidgetItem;
    item->setText("danny");
    ui->tablewidget->setItem(2, 2, item);


Log in to reply