Results 1 to 19 of 19

Thread: QTreeView: How to refresh the view?

  1. #1
    Join Date
    Mar 2012
    Posts
    5

    Default QTreeView: How to refresh the view?

    Hi,

    I am using a QTreeView with an QAbstractItemModel to display my data.
    My data changes sometimes and I need to inform the QTreeView about the changed data, how do I do this?

    For example, when I first call setModel() on the QTreeView there is no data.
    After this I add ~20 items to the data list.. but the QTreeView doesn't refresh...

    I tried to emit "dataChanged" and "layoutChanged", but it didn't work.. my current workarround is:

    Qt Code:
    1. treeView->setModel(NULL);
    2. treeView->setModel(&model);
    To copy to clipboard, switch view to plain text mode 

    what's the correct way to do this?

    Kira

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView: How to refresh the view?

    You have to use beginInsertRows() and endInsertRows() while adding data to the model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2012
    Posts
    5

    Default Re: QTreeView: How to refresh the view?

    Okay, will try this.

    And is there a way to do a complete refresh? Because sometimes there isn't only a simple row insertion, but a complete update of the whole data.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView: How to refresh the view?

    If you see doc for beginInsertRows and endInstertRows as Wysota said, you will notice its ROWS, not ROW.
    So you can specify the indexes accordingly.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView: How to refresh the view?

    Quote Originally Posted by Kira View Post
    And is there a way to do a complete refresh?
    Yes, there is. Look at the methods and signals available for QAbstractItemModel.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Mar 2012
    Posts
    5

    Default Re: QTreeView: How to refresh the view?

    I did look at the methods and signals of QAbstractItemModel already, all I found was "dataChanged" and "layoutChanged".. but this didn't work (Or I used it wrong?).

    About beginInsertRows/endInsertRows: When I am adding 20 rows where each row has 10 children.. how would I call beginInsertRows? What do I pass as parent? The rows I am adding in a batch have different parents. Or do I call this method only for the top-level hirarchy?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView: How to refresh the view?

    Quote Originally Posted by Kira View Post
    I did look at the methods and signals of QAbstractItemModel already, all I found was "dataChanged" and "layoutChanged".. but this didn't work (Or I used it wrong?).
    Then look again, I see 18 signals in the class.

    About beginInsertRows/endInsertRows: When I am adding 20 rows where each row has 10 children.. how would I call beginInsertRows?
    If you do that in one go then you just call it for the parent rows. The view will notice the children by itself then.

    The rows I am adding in a batch have different parents. Or do I call this method only for the top-level hirarchy?
    You can call those functions more than once, you know.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2012
    Posts
    5

    Default Re: QTreeView: How to refresh the view?

    Wysota, why do you count the signals instead of simply telling me which one I need?!

    I know there are 18 and as I said I already tried the two that make sense for me: dataChanged() and layoutChanged() (of course including layoutAboutToBeChanged() as it's written in the docs).. so am I missing something? If yes, would you please be so kind and tell me?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView: How to refresh the view?

    Quote Originally Posted by Kira View Post
    Wysota, why do you count the signals instead of simply telling me which one I need?!
    I already told you once and you were not able to extrapolate on the knowledge you were given. I will tell you now again and then you will come back in two hours asking what to do if you want to delete rows from the model. The docs are not that long, it's much quicker to read through the whole page instead of bouncing around forums and waiting for people to point you back to the docs. It's also much quicker to look at one of the numerous great examples available in the docs.

    I know there are 18 and as I said I already tried the two that make sense for me: dataChanged() and layoutChanged() (of course including layoutAboutToBeChanged() as it's written in the docs).. so am I missing something? If yes, would you please be so kind and tell me?
    An obvious one to try would be modelReset(), wouldn't it? If you start there, you will certainly come up with a proper sequence of calls you need to make to reset a model, especially since you were already given information on how to add rows to the model.
    Last edited by wysota; 30th March 2012 at 13:09.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    ednakram (26th June 2017)

  11. #10
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView: How to refresh the view?

    wysota's responses like this come up on google searches. They should all be deleted. Almost always useless in trying to find a quick answer to a well formulated question.

  12. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView: How to refresh the view?

    Quote Originally Posted by philw View Post
    wysota's responses like this come up on google searches. They should all be deleted. Almost always useless in trying to find a quick answer to a well formulated question.
    I am afraid I don't understand.
    What would a search result of a question without answers be good for?

    Cheers,
    _

  13. #12
    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: QTreeView: How to refresh the view?

    Wysota:

    Join Date Jan 2006
    Posts 31,660

    This sort of speaks for itself. I think Wysota can be forgiven if he sometimes doesn't respond well to people who expect to be spoon-fed code in answer to their questions.

  14. #13
    Join Date
    Aug 2011
    Posts
    1
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView: How to refresh the view?

    I know this question was originally asked something like 2.5 years ago, but I've recently been trying to teach myself Qt (PyQt, actually), and I had this same question, which is how I found this post. Anyway...

    In my case, I periodically update an underlying data set with raw data. My QAbstractItemModel-derived data model gets its data from this underlying data set. So, I ran into this issue of trying to figure out how to get the view to update every time the underlying data was updated. After a couple days of researching this on the web and trying various things, I came up with the very simple approach of just adding a method, called emitDataChanged(), to my model subclass. Here's the Python code for this method:

    def emitDataChanged(self):
    self.dataChanged.emit(QModelIndex(), QModelIndex())

    From what I understand, this signal tells the view that "all the data has changed", so the view repaints everything. In the code that updates the underlying data, once all the data has been updated, I just call this method on the model, and the view automagically updates.

    The nice thing about this approach is that the current state of the view remains intact. The approaches I tried in the beginning involved the whole beginModelReset/endModelReset thing, and although that caused the view to update, it also caused any expanded nodes to be collapsed, such that only the top-level nodes were visible after the update. It also caused the currently selected item to be lost. Using the dataChanged.emit(...) approach caused the view to behave exactly as I wanted it to: just repaint the whole thing. No selection lost, no nodes collapsed.

    So that's it. Just thought I'd share in the hopes that this might help someone else in the future. Good luck.

  15. The following 4 users say thank you to ttrop for this useful post:

    d_stranz (19th December 2014), Kryzon (25th January 2015), Rafe (30th August 2017), t_3 (24th January 2015)

  16. #14
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTreeView: How to refresh the view?

    Quote Originally Posted by ttrop View Post
    I know this question was originally asked something like 2.5 years ago, ....
    another 3.5 years later i'd like to thank you for sharing this find. i also had a case where some underlying data constantly changes, but even the structure of the tree stays static. now a single one-liner allows updating hundreds of rows (driven by slider inputs) without the need to recreate the whole thing...

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView: How to refresh the view?

    Just remember this is ok-ish for some models (and their use) but not for others. It's not a magic wand you can use in any case. There is more to it than redrawing a view when a model is modified.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #16
    Join Date
    Oct 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView: How to refresh the view?

    Another 3.5 years late also I'd like to thank you very much for sharing this. What a wonderful method!

  19. #17
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QTreeView: How to refresh the view?

    You can also do model.reset() which forces all the views to update themselves, refetch all the visible data.

    Emitting the dataChanged signal is better if you are just making minor changes.

    This is from Summerfield's PyQt book:
    Sorting the data makes all model indexes invalid and means that the views are now showing the wrong data. The model must notify the views that they need to update themselves by retrieving fresh data. One way to do this is to emit a dataChanged() signal, but for big changes it is more efficient to call QAbstractTableModel.reset(); this tells all associated views that everything is out-of-date and forces them to update themselves.
    Note: Use beginResetModel() and endResetModel() instead whenever possible. Use reset only if there is no way to call beginResetModel() before invalidating the model. Otherwise it could lead to unexpected behaviour, especially when used with proxy models.
    Last edited by neuronet; 11th October 2015 at 03:09.

  20. #18
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: QTreeView: How to refresh the view?

    Quote Originally Posted by ttrop View Post
    self.dataChanged.emit(QModelIndex(), QModelIndex())
    Strangely when I run that from my model, my delegate doesn't actually call the sizeHint method, which is what I need, it only calls the paint method in my delegate. So this doesn't work for me. Calling model.layoutChanged.emit() works though.

  21. #19
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTreeView: How to refresh the view?

    Quote Originally Posted by neuronet View Post
    Note: Use beginResetModel() and endResetModel() instead whenever possible. Use reset only if there is no way to call beginResetModel() before invalidating the model. Otherwise it could lead to unexpected behaviour, especially when used with proxy models.
    very helpful & important advice;
    when using proxy models this is imo the only way to properly update everything up to the view(s), when populating the model with all new content.

    Quote Originally Posted by neuronet View Post
    Strangely when I run that from my model, my delegate doesn't actually call the sizeHint method, which is what I need, it only calls the paint method in my delegate. So this doesn't work for me. Calling model.layoutChanged.emit() works though.
    sounds reasonable; repainting the whole view (because of layoutChanged()) is probably more expensive than just repainting the cells, so one can decide what to do, depending how the inner data changes, or the layout is organized...

Similar Threads

  1. [solved]My SqlModel and refresh view
    By Hostel in forum Newbie
    Replies: 0
    Last Post: 15th September 2011, 22:55
  2. Replies: 3
    Last Post: 18th March 2010, 04:49
  3. How to constantly refresh time on a view
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2008, 12:44
  4. refresh Qtreeview after removing row
    By doublelune in forum Qt Programming
    Replies: 4
    Last Post: 13th March 2008, 18:52
  5. Replies: 9
    Last Post: 7th November 2006, 15:10

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.