Reputation: 490
I have an invoice form which i am using to both create invoices and display the results of a stored invoice. I when i am trying to read back data from the database and display it i am getting the error of
QTableWidget: cannot insert an item that is already owned by another QTableWidget QTableWidget: cannot insert an item that is already owned by another QTableWidget QTableWidget: cannot insert an item that is already owned by another QTableWidget QTableWidget: cannot insert an item that is already owned by another QTableWidget QTableWidget: cannot insert an item that is already owned by another QTableWidget
I don't understand why this is being cause. The query that i am trying to run will return 4 rows. My code is below
QString CompanyID;
int row = 0;
ui->comboBox_Invoice_Account_Search->setCurrentIndex(1);
QSqlQuery Account_Name_Query;
QTableWidgetItem *Qty_Search = new QTableWidgetItem();
QTableWidgetItem *Description_Search = new QTableWidgetItem();
QTableWidgetItem *Product_Code_Search = new QTableWidgetItem();
QTableWidgetItem *Unit_Price_Search = new QTableWidgetItem();
QTableWidgetItem *Total_Price_Search = new QTableWidgetItem();
while(Query.next())
{
CompanyID = Query.value(10).toString();
//qDebug() << "CompanyID " << CompanyID;
ui->lineEdit_Invoice_VAT->setText(Query.value(9).toString());
ui->lineEdit_Invoice_Total->setText(Query.value(8).toString());
ui->lineEdit_Goods_Total->setText(Query.value(7).toString());
Qty_Search->setText(Query.value(3).toString());
Description_Search->setText(Query.value(4).toString());
Product_Code_Search->setText(Query.value(5).toString());
Unit_Price_Search->setText(Query.value(6).toString());
Total_Price_Search->setText(Query.value(7).toString());
ui->tableWidget_Invoice->setItem(row, 0, Qty_Search);
ui->tableWidget_Invoice->setItem(row, 1, Description_Search);
ui->tableWidget_Invoice->setItem(row, 2, Product_Code_Search);
ui->tableWidget_Invoice->setItem(row, 3, Unit_Price_Search);
ui->tableWidget_Invoice->setItem(row, 4, Total_Price_Search);
row++;
Account_Name_Query.prepare("SELECT Company_Name FROM Customer WHERE Company_ID = '"+ CompanyID +"'");
Account_Name_Query.exec();
while(Account_Name_Query.next())
{
ui->lineEdit_Invoice_Account->setText(Account_Name_Query.value(0).toString());
}
}
what is causing this error?
Upvotes: 3
Views: 4994
Reputation: 10456
That's because you are trying to set the same items you created outside of loop into the same table multiple times. Change your code to:
while(Query.next())
{
QTableWidgetItem *Qty_Search = new QTableWidgetItem();
QTableWidgetItem *Description_Search = new QTableWidgetItem();
QTableWidgetItem *Product_Code_Search = new QTableWidgetItem();
QTableWidgetItem *Unit_Price_Search = new QTableWidgetItem();
QTableWidgetItem *Total_Price_Search = new QTableWidgetItem();
...
}
Upvotes: 5