QStandardItemModel - delete a line

I am using QStandardItemModel inside a QTableView . Here I have two buttons and a QTableView inside my main window. Lines will change inside the model. Two buttons for adding / deleting a line (test script).

Adding a line to the model works, the slot for the ADD button : -

 void MainWindow::on_pushButton_clicked() { model->insertRow(model->rowCount()); } 

But my program crashes when I delete a line from the model, slot for 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 need to change in my code to remove the job.

Edit: ----

It worked.

 QModelIndex currentIndex = ui->tableView->selectionModel()->currentIndex(); model->removeRow(currentIndex.row()); 
+4
source share
1 answer

My suggestion is that you are trying to delete a row without highlighting. Try the following:

 void MainWindow::on_pushButton_2_clicked() { QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows(); while (!indexes.isEmpty()) { model->removeRows(indexes.last().row(), 1); indexes.removeLast(); } } 
+2
source

Source: https://habr.com/ru/post/1484818/


All Articles