Updating a data in QTableView question
Hi to all, especialy nearly divine entities like jacek, jpn, marcel, wysota and others! :D:D
So, like I said, I have a shopping cart of merchandize in QTableView. Record are now added as they should, but I bumped into following problem. Let's say the shopping cart contains:
Code:
+========================+
+ Coca Cola +
+ 1,00 1x 1.00 +
+------------------------+
+ Sprite +
+ 2.44 1x 2.44 +
+========================+
Now, the user chooses another sprite and instead of:
Code:
+========================+
+ Coca Cola +
+ 1,00 1x 1.00 +
+------------------------+
+ Sprite +
+ 2.44 1x 2.44 +
+------------------------+
+ Sprite +
+ 2.44 1x 2.44 +
+========================+
I would like updated quantity and subtotal (second line, second number is quantity, third number is subtotal) like this:
Code:
+========================+
+ Coca Cola +
+ 1,00 1x 1.00 +
+------------------------+
+ Sprite +
+ 2.44 2x 4.88 +
+========================+
Now, here is a code chunck that should do this:
Code:
else
{
// merchandize found, update quantity and subtotal
qDebug() << "iRemberedMerchandizeID:" << iRemberedMerchandizeID; // debug output
CMerchandizeOrder* pUpdatedOrder=new CMerchandizeOrder(order); // gets order to update
Q_CHECK_PTR(pUpdatedOrder); // checks creation
structOrder updatedOrderStruct=pUpdatedOrder->orderValues(); // gets order's struct
updatedOrderStruct.iMerchandizeQuantity++; // increase quantity
updatedOrderStruct.rSubtotal=updatedOrderStruct.iMerchandizeQuantity*updatedOrderStruct.rMerchandizePrice; // updates subtotal
pUpdatedOrder->setMerchandizeQuantity(updatedOrderStruct.iMerchandizeQuantity);
pUpdatedOrder->setSubtotal(updatedOrderStruct.rSubtotal);
pUpdatedOrder->setDisplayString(updatedOrderStruct);
m_pShoppingCartModel->orders().replace(iRememberedIndex, pUpdatedOrder);
shoppingCartModel()->setData(index,
pUpdatedOrder->orderValues().strDisplayString,
Qt::EditRole);
emit dataChanged(index, index);
}
// TODO: update grand total field
but the cell is not updated, i.e., nothing happens. The code is a part of bigger if clause, and I've set breakpoint in the begining of this code chunk to check if the IF clause works ok. It does. I've checked with debugger, data are recalculated correctly (quantity and subtotal), but the contenst of QTableView are not updatet. Why?!
Re: Updating a data in QTableView question
No one has idea what is wrong?
Re: Updating a data in QTableView question
Why is dataChanged emitted from this function. I thought it is called from setData() ?!
I found this post. Maybe this is something similar.
Quote:
QSqlQueryModel is read only, that's one thing. Changing the record doesn't modify the database (record() returns a copy of the record), that's another thing.
http://www.qtcentre.org/forum/f-qt-p...lue-11762.html
Actually i have the same problem: Can not change a Record filed value inside of setData in a subclassed QueryModel.
Re: Updating a data in QTableView question
Sorry, I forgot to delete emitData(). You were right, emitData is called from my setData(). So, my model is subclassed from QAbstractTableModel, you think I have to make it editable for operation I want to achieve?
Re: Updating a data in QTableView question
And how to delete item from model (from tableview) if necessary. I do not know how to get index (row) of selected item.
Re: Updating a data in QTableView question
Quote:
Originally Posted by
MarkoSan
And how to delete item from model (from tableview) if necessary.
Use QAbstractItemModel::removeRows(), or if you're using QStandardItems I believe you can simply delete them.
Quote:
I do not know how to get index (row) of selected item.