I am using QStandardItemModel
inside QTableView
. Here I have two button & QTableView
inside my mainwindow. Rows will vary inside the model.
Two Buttons are there to add/delete a row (test case).
Adding row to the model is working, slot for the ADD button
:--
void MainWindow::on_pushButton_clicked()
{
model->insertRow(model->rowCount());
}
But my program is crashing when I am deleting a row from the model, slot for the Delete button
:--
void MainWindow::on_pushButton_2_clicked()
{
QModelIndexList indexes = ui->tableView->selectionModel()->selection().indexes();
QModelIndex index = indexes.at(0);
model->removeRows(index.row(),1);
}
Please suggest what I have to change in my code to make delete working.
Edit :----
Got it working.
QModelIndex currentIndex = ui->tableView->selectionModel()->currentIndex();
model->removeRow(currentIndex.row());
My suggestion is - you are trying to delete row without selection. Try this:
void MainWindow::on_pushButton_2_clicked()
{
QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
while (!indexes.isEmpty())
{
model->removeRows(indexes.last().row(), 1);
indexes.removeLast();
}
}