Results 1 to 3 of 3

Thread: Refreshing QTableView data without resetting whole view

  1. #1
    Join Date
    Feb 2008
    Posts
    79
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Refreshing QTableView data without resetting whole view

    I have another problem, that I try to solve for a while now, but without (real) success.

    Let's assume we have QTableView that is populated by QSqlQueryModel data. Now when I change some data inside model, I want to refresh view too. If I do this with running setQuery again, then view does refresh itself, but(!) the whole view also resets itself. This is really annoying if user looks at a long table, and for example has one row selected. When view refreshes, his selection is gone and he must go looking for that particular row again. So I would need to refresh view without resetting the whole thing. How do I do that?
    Last edited by Tiansen; 1st February 2014 at 22:46.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Refreshing QTableView data without resetting whole view

    Think about the general case of your problem: you have a table view connected to an SQL query model, you issue new queries on the database, and you repopulate the view with the results. In the general case, the new quesry results will have nothing in common with the previous results, so re-selecting the same row afterwards makes no sense at all, because the contents of the row will probably be completely different.

    On the other hand, if the query always returns the same rows, but the contents of the fields within the row might be updated, then you have a more restricted case and it might make sense to keep track of the current row and re-select it after the update.

    Are you using a QItemSelectionModel on the view? You can use it to keep track of the current selection, and then after the refresh, use it to select the same row again.

    Also remember that QModelIndex instances are transient. If the model refreshes because you've run a new query, then none of the indexes from the previous model will be valid. You'll need to keep track of the actual row number, and use it to construct a QModelIndex (using QAbstractItemModel::createIndex()) that you can pass into the QItemSelectionModel::select() slot.

    Question for the admins: why is it that all of the QTCLASS references go to the docs for Qt 4.8 instead of the current Qt5 release?
    Last edited by d_stranz; 2nd February 2014 at 03:18.

  3. The following user says thank you to d_stranz for this useful post:

    Tiansen (2nd February 2014)

  4. #3
    Join Date
    Feb 2008
    Posts
    79
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Refreshing QTableView data without resetting whole view

    Very good, d_stranz. I solved it as you suggested. Thank you very much!

    Do you maybe have an idea how to solve my another issue?
    Link: http://www.qtcentre.org/threads/5789...ws-and-sorting

Similar Threads

  1. refreshing QSqlRelationalDelegate data
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2010, 01:54
  2. Get data from a QTableView
    By graciano in forum Qt Programming
    Replies: 3
    Last Post: 19th January 2010, 16:31
  3. Insert data to QTableView
    By Lodhart in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2009, 10:38
  4. Replies: 3
    Last Post: 29th January 2009, 09:38
  5. QTableView not refreshing
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2007, 20:37

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.