Kito. The itemModel is the same one since it was declared in the header file as a private. In the add function it works fine so I think that is not the problem. I tried printing an item from the itemModel and it prints out correctly.
Chris. Index returns the correct index of 1 if I select the second item in the qtableview.
void addOrder::on_deleteItemPushButton_clicked()
{
// Delete the selected item
int index = ui->itemsTableView->currentIndex().row();
//QModelIndex index2 = ui->itemsTableView->currentIndex();
qDebug() << "Value of Item at 0,1" << itemModel->data(itemModel->index(0,1), Qt::DisplayRole).toString();
qDebug() << "Value of index" << index;
qDebug() << "Primary Key" << itemModel->primaryKey();
if (!itemModel->removeRow(index))
qDebug() << itemModel->lastError();
ui->savePushButton->setEnabled(true);
}
void addOrder::on_deleteItemPushButton_clicked()
{
// Delete the selected item
int index = ui->itemsTableView->currentIndex().row();
//QModelIndex index2 = ui->itemsTableView->currentIndex();
qDebug() << "Value of Item at 0,1" << itemModel->data(itemModel->index(0,1), Qt::DisplayRole).toString();
qDebug() << "Value of index" << index;
qDebug() << "Primary Key" << itemModel->primaryKey();
if (!itemModel->removeRow(index))
qDebug() << itemModel->lastError();
ui->savePushButton->setEnabled(true);
}
To copy to clipboard, switch view to plain text mode
Value of Item at 0,1 "5"
Value of index 1
" 0:" QSqlField("Orders_OrderID",
int, length
: 11, precision
: 0, required
: yes, generated
: yes,
typeID: 3) "" " 1:" QSqlField("Products_ProductID",
int, length
: 11, precision
: 0, required
: yes, generated
: yes,
typeID: 3) "" QSqlQuery::value: not positioned on a valid record
Value of Item at 0,1 "5"
Value of index 1
Primary Key QSqlRecord( 2 )
" 0:" QSqlField("Orders_OrderID", int, length: 11, precision: 0, required: yes, generated: yes, typeID: 3) ""
" 1:" QSqlField("Products_ProductID", int, length: 11, precision: 0, required: yes, generated: yes, typeID: 3) ""
QSqlQuery::value: not positioned on a valid record
To copy to clipboard, switch view to plain text mode
I have seen some other posts that have a problem with composite keys. Any ideas?
Added after 1 29 minutes:
I think I have pinpointed my problem.
itemModel
->setRelation
(2,
QSqlRelation("products",
"ProductID",
"ProductName"));
itemModel->setRelation(2, QSqlRelation("products", "ProductID", "ProductName"));
To copy to clipboard, switch view to plain text mode
I am setting the relationship of the product id with the product name so in the itemTableView I have the names of the products and not the ids. If I remove this line the delete happens correctly. Now to find out how to put the names and then use the name to get the ID to use in the delete function.
Is there a way to get the actual ProductID instead of the ProductName which substitutes it in the qtableview?
Thanks,
Pericles
Bookmarks