I have this signal on QPushButton:
void MainWindow::on_addNode_clicked()
{
ui->nodesTable->insertRow(ui->nodesTable->rowCount());
if(ui->nodesTable->rowCount()>1)
{
ui->nodesTable->item(ui->nodesTable->rowCount()-1, 0)->setText(ui->nodesTable->item(ui->nodesTable->rowCount()-2,0)->text());
}
else
{
ui->nodesTable->item(ui->nodesTable->rowCount()-1, 0)->setText(QString::fromStdString("0"));
}
}
But it crashes with segmentation fault each time when I click the button. As I understood, item(...) give nullptr but why?
at the first time there is no item in your table, your application crashed if pointer pointed non and when you want to reach it!
try this;
void MainWindow::on_addNode_clicked()
{
ui->nodesTable->insertRow(ui->nodesTable->rowCount());
if( !ui->nodesTable->rowCount() )
{
ui->nodesTable->item(ui->nodesTable->rowCount()-1, 0)->setText(ui->nodesTable->item(ui->nodesTable->rowCount()-2,0)->text());
}
else
{
ui->nodesTable->setItem(0, 0,new QTableWidgetItem(QString("New Item")));
}
}