PDA

View Full Version : Weird removeRow behavior



metalinspired
30th August 2009, 12:42
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:

QSqlRecord artikl = modelArtikl->record(proxyArtikl->mapToSource(proxyArtikl->index(sifraCombo->currentIndex(), 0)).row());
QSqlRecord artiklRacun = modelRacun->record();
QVariant pomocna;

artiklRacun.setValue(0, artikl.value(0));
artiklRacun.setValue(1, artikl.value(1));
artiklRacun.setValue(2, artikl.value(2));
artiklRacun.setValue(3, artikl.value(3));
artiklRacun.setValue(4, artikl.value(4));
artiklRacun.setValue(5, artikl.value(5));
pomocna = kolicinaLineEdit->text().toDouble();
artiklRacun.setValue(6, QString::number(pomocna.toDouble(), 'f', 3).replace('.',','));
pomocna = artikl.value(4).toDouble() * kolicinaLineEdit->text().toDouble();
QString ukupno = pomocna.toString();
artiklRacun.setValue(7, QString::number(ukupno.toDouble(), 'f', 2).replace('.',','));

modelRacun->insertRecord(-1, artiklRacun);
modelRacun->submit();

And here is the part that deletes row from model:

if(watched == racunTableView)
{
if(e->type() == QEvent::KeyPress)
{
QKeyEvent *tipka = static_cast<QKeyEvent*>(e);
if(tipka->key() == Qt::Key_Delete)
if(racunTableView->currentIndex().isValid())
{
QMessageBox upit;
upit.setIcon(QMessageBox::Question);
upit.setWindowTitle(tr("Upit"));
upit.setText(tr("Do you want to do this?"));
QPushButton *daButton = upit.addButton(tr("Yes"), QMessageBox::YesRole);
upit.addButton(tr("No"), QMessageBox::NoRole);
upit.setDefaultButton(QMessageBox::Yes);
upit.exec();
if(upit.clickedButton() == daButton)
{
modelRacun->removeRow(racunTableView->currentIndex().row());
}
}

}
}