Hi all.
Yesterday I wrote a function that deletes a row from a table when user presses Delete on his keyboard. It all seemed to work as expected, but then I accidently clicked add button few times thus adding a few identical entries to table data model (QSqlTableModel). When I select one of those entries and click Delete I get all those identical entries deleted, even if I only wanted to delete one. Here’s some code…

Here is the part of code that adds data from one model to the one showed in tableView:
Qt Code:
  1. QSqlRecord artikl = modelArtikl->record(proxyArtikl->mapToSource(proxyArtikl->index(sifraCombo->currentIndex(), 0)).row());
  2. QSqlRecord artiklRacun = modelRacun->record();
  3. QVariant pomocna;
  4.  
  5. artiklRacun.setValue(0, artikl.value(0));
  6. artiklRacun.setValue(1, artikl.value(1));
  7. artiklRacun.setValue(2, artikl.value(2));
  8. artiklRacun.setValue(3, artikl.value(3));
  9. artiklRacun.setValue(4, artikl.value(4));
  10. artiklRacun.setValue(5, artikl.value(5));
  11. pomocna = kolicinaLineEdit->text().toDouble();
  12. artiklRacun.setValue(6, QString::number(pomocna.toDouble(), 'f', 3).replace('.',','));
  13. pomocna = artikl.value(4).toDouble() * kolicinaLineEdit->text().toDouble();
  14. QString ukupno = pomocna.toString();
  15. artiklRacun.setValue(7, QString::number(ukupno.toDouble(), 'f', 2).replace('.',','));
  16.  
  17. modelRacun->insertRecord(-1, artiklRacun);
  18. modelRacun->submit();
To copy to clipboard, switch view to plain text mode 

And here is the part that deletes row from model:
Qt Code:
  1. if(watched == racunTableView)
  2. {
  3. if(e->type() == QEvent::KeyPress)
  4. {
  5. QKeyEvent *tipka = static_cast<QKeyEvent*>(e);
  6. if(tipka->key() == Qt::Key_Delete)
  7. if(racunTableView->currentIndex().isValid())
  8. {
  9. upit.setIcon(QMessageBox::Question);
  10. upit.setWindowTitle(tr("Upit"));
  11. upit.setText(tr("Do you want to do this?"));
  12. QPushButton *daButton = upit.addButton(tr("Yes"), QMessageBox::YesRole);
  13. upit.addButton(tr("No"), QMessageBox::NoRole);
  14. upit.setDefaultButton(QMessageBox::Yes);
  15. upit.exec();
  16. if(upit.clickedButton() == daButton)
  17. {
  18. modelRacun->removeRow(racunTableView->currentIndex().row());
  19. }
  20. }
  21.  
  22. }
  23. }
To copy to clipboard, switch view to plain text mode