Results 1 to 6 of 6

Thread: Updating a data in QTableView question

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Updating a data in QTableView question

    Hi to all, especialy nearly divine entities like jacek, jpn, marcel, wysota and others!

    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:
    Qt Code:
    1. +========================+
    2. + Coca Cola +
    3. + 1,00 1x 1.00 +
    4. +------------------------+
    5. + Sprite +
    6. + 2.44 1x 2.44 +
    7. +========================+
    To copy to clipboard, switch view to plain text mode 
    Now, the user chooses another sprite and instead of:
    Qt Code:
    1. +========================+
    2. + Coca Cola +
    3. + 1,00 1x 1.00 +
    4. +------------------------+
    5. + Sprite +
    6. + 2.44 1x 2.44 +
    7. +------------------------+
    8. + Sprite +
    9. + 2.44 1x 2.44 +
    10. +========================+
    To copy to clipboard, switch view to plain text mode 
    I would like updated quantity and subtotal (second line, second number is quantity, third number is subtotal) like this:
    Qt Code:
    1. +========================+
    2. + Coca Cola +
    3. + 1,00 1x 1.00 +
    4. +------------------------+
    5. + Sprite +
    6. + 2.44 2x 4.88 +
    7. +========================+
    To copy to clipboard, switch view to plain text mode 
    Now, here is a code chunck that should do this:
    Qt Code:
    1. else
    2. {
    3. // merchandize found, update quantity and subtotal
    4. qDebug() << "iRemberedMerchandizeID:" << iRemberedMerchandizeID; // debug output
    5. CMerchandizeOrder* pUpdatedOrder=new CMerchandizeOrder(order); // gets order to update
    6. Q_CHECK_PTR(pUpdatedOrder); // checks creation
    7. structOrder updatedOrderStruct=pUpdatedOrder->orderValues(); // gets order's struct
    8.  
    9. updatedOrderStruct.iMerchandizeQuantity++; // increase quantity
    10. updatedOrderStruct.rSubtotal=updatedOrderStruct.iMerchandizeQuantity*updatedOrderStruct.rMerchandizePrice; // updates subtotal
    11. pUpdatedOrder->setMerchandizeQuantity(updatedOrderStruct.iMerchandizeQuantity);
    12. pUpdatedOrder->setSubtotal(updatedOrderStruct.rSubtotal);
    13. pUpdatedOrder->setDisplayString(updatedOrderStruct);
    14. m_pShoppingCartModel->orders().replace(iRememberedIndex, pUpdatedOrder);
    15. QModelIndex index=shoppingCartModel()->index(0, 0, QModelIndex());
    16. shoppingCartModel()->setData(index,
    17. pUpdatedOrder->orderValues().strDisplayString,
    18. Qt::EditRole);
    19. emit dataChanged(index, index);
    20. }
    21. // TODO: update grand total field
    To copy to clipboard, switch view to plain text mode 
    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?!
    Last edited by MarkoSan; 28th May 2008 at 21:18. Reason: spelling error
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Updating a data in QTableView question

    No one has idea what is wrong?
    Qt 5.3 Opensource & Creator 3.1.2

  3. #3
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default 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.
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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?
    Qt 5.3 Opensource & Creator 3.1.2

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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.
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Updating a data in QTableView question

    Quote Originally Posted by MarkoSan View Post
    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.
    J-P Nurmi

Similar Threads

  1. QTableView and its Scrollbar question
    By MarkoSan in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2008, 14:23
  2. QTableview data display font question
    By MarkoSan in forum Newbie
    Replies: 8
    Last Post: 14th May 2008, 12:57
  3. QTableView - data() - item
    By starcontrol in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2008, 14:41
  4. finding data in a qtableview
    By JeanC in forum Newbie
    Replies: 4
    Last Post: 7th March 2008, 19:36
  5. QTableView Question
    By kandalf in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2007, 18:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.