PDA

View Full Version : Updating a data in QTableView question



MarkoSan
28th May 2008, 21:16
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:
+========================+
+ Coca Cola +
+ 1,00 1x 1.00 +
+------------------------+
+ Sprite +
+ 2.44 1x 2.44 +
+========================+Now, the user chooses another sprite and instead of:
+========================+
+ 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:
+========================+
+ Coca Cola +
+ 1,00 1x 1.00 +
+------------------------+
+ Sprite +
+ 2.44 2x 4.88 +
+========================+Now, here is a code chunck that should do this:
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.iM erchandizeQuantity*updatedOrderStruct.rMerchandize Price; // updates subtotal
pUpdatedOrder->setMerchandizeQuantity(updatedOrderStruct.iMerchan dizeQuantity);
pUpdatedOrder->setSubtotal(updatedOrderStruct.rSubtotal);
pUpdatedOrder->setDisplayString(updatedOrderStruct);
m_pShoppingCartModel->orders().replace(iRememberedIndex, pUpdatedOrder);
QModelIndex index=shoppingCartModel()->index(0, 0, QModelIndex());
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?!

MarkoSan
29th May 2008, 18:51
No one has idea what is wrong?

janus
29th May 2008, 19:14
Why is dataChanged emitted from this function. I thought it is called from setData() ?!

I found this post. Maybe this is something similar.
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-programming-2/t-qsqlrecord-setvalue-doesnt-set-the-value-11762.html

Actually i have the same problem: Can not change a Record filed value inside of setData in a subclassed QueryModel.

MarkoSan
29th May 2008, 19:25
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?

MarkoSan
4th June 2008, 12:46
And how to delete item from model (from tableview) if necessary. I do not know how to get index (row) of selected item.

jpn
6th June 2008, 11:45
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.


I do not know how to get index (row) of selected item.

QAbstractItemView::selectedIndexes()
QAbstractItemView::selectionModel()