pcheng
25th June 2012, 10:29
I have a qtableview which shows the values of a QSqlModel. Adding an item works fine. When I try to delete an item I get the error "QSqlQuery::value: not positioned on a valid record".
The itemModel is instantiated as follows:
// Setup the Order Items View
itemModel = new QSqlRelationalTableModel(this);
itemModel->setTable("orderitem");
itemModel->setRelation(2, QSqlRelation("products", "ProductID", "ProductName"));
ui->itemsTableView->setModel(itemModel);
ui->itemsTableView->setItemDelegate(new QSqlRelationalDelegate(itemModel));
ui->itemsTableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->itemsTableView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->itemsTableView->setColumnHidden(1, true);
ui->itemsTableView->setAlternatingRowColors(true);
The delete button code is as follows:
void addOrder::on_deleteItemPushButton_clicked()
{
// Delete the selected item
int index = ui->itemsTableView->currentIndex().row();
if (!itemModel->removeRow(index))
qDebug() << itemModel->lastError();
ui->savePushButton->setEnabled(true);
}
Any ideas on why the delete does not work?
I am thinking that maybe the problem lies in the fact that the table in MySQL is actually a combined primary key rather than a single key. It takes the ProductID and the OrderID as a combo primary key. I am considering this problem because on another class which has a single key parameter the same code works fine. Is it possible to send two values to delete a row?
Thanks,
Pericles
The itemModel is instantiated as follows:
// Setup the Order Items View
itemModel = new QSqlRelationalTableModel(this);
itemModel->setTable("orderitem");
itemModel->setRelation(2, QSqlRelation("products", "ProductID", "ProductName"));
ui->itemsTableView->setModel(itemModel);
ui->itemsTableView->setItemDelegate(new QSqlRelationalDelegate(itemModel));
ui->itemsTableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->itemsTableView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->itemsTableView->setColumnHidden(1, true);
ui->itemsTableView->setAlternatingRowColors(true);
The delete button code is as follows:
void addOrder::on_deleteItemPushButton_clicked()
{
// Delete the selected item
int index = ui->itemsTableView->currentIndex().row();
if (!itemModel->removeRow(index))
qDebug() << itemModel->lastError();
ui->savePushButton->setEnabled(true);
}
Any ideas on why the delete does not work?
I am thinking that maybe the problem lies in the fact that the table in MySQL is actually a combined primary key rather than a single key. It takes the ProductID and the OrderID as a combo primary key. I am considering this problem because on another class which has a single key parameter the same code works fine. Is it possible to send two values to delete a row?
Thanks,
Pericles