Results 1 to 12 of 12

Thread: Self-update QAbstractItemDelegate when item changes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Self-update QAbstractItemDelegate when item changes

    Here are the main points:
    • The view will not update itself unless the model tells it the data has changed or the user triggers a view change that forces it to refetch the data.
    • The model cannot tell the view that underlying data has changed unless it knows itself
    • You are changing the underlying data from outside the model and not informing the model.


    The usual approach would be, as wysota points out, to put the actual data inside the model and maintain the state of the data through the model instance. You can use the setData() interface to the model, or your own methods, but whenever the model changes the data it must emit the relevant dataChanged() or related signals. When your network reply comes in you insert the new image into the model causing the model to emit dataChanged().

  2. The following user says thank you to ChrisW67 for this useful post:

    jiveaxe (13th February 2013)

  3. #2
    Join Date
    Aug 2007
    Posts
    244
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    42
    Thanked 8 Times in 8 Posts

    Default Re: Self-update QAbstractItemDelegate when item changes

    Quote Originally Posted by ChrisW67 View Post
    The usual approach would be, as wysota points out, to put the actual data inside the model and maintain the state of the data through the model instance. You can use the setData() interface to the model, or your own methods, but whenever the model changes the data it must emit the relevant dataChanged() or related signals. When your network reply comes in you insert the new image into the model causing the model to emit dataChanged().
    I've followed your and wysota way. I subclassed QNetworkAccessManager and now the model calls it with url and index; this new class downloads the image and emits a signal intercepted by the model that in turn emits dataChanged(), as you recommend. Now images are self-updated, as expected. I only encountered a small bug: seems that the model calls the download class 4 times per item and when items are a lot the program crashes with this message:

    GLib-ERROR **: Creating pipes for GWakeup: Too many open files
    Is there a way to limit model's calls? Or is it best to implement a download manager with queue support?

    Thank you both for the valuable help.
    Giuseppe CalÃ

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Self-update QAbstractItemDelegate when item changes

    The model's data() method will be called multiple times for each model index in the view: for the DisplayRole, DecorationRole, SizeHintRole etc... If you fetch the image once for each call you will indeed be performing efficiency crimes. Make sure you are only fetching a remote image once and storing it in the model for later use.

Similar Threads

  1. Single Item update in QGraphicsView
    By oberlus in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2010, 09:17
  2. Replies: 2
    Last Post: 9th August 2010, 17:53
  3. QAbstractItemDelegate and QLinearGradient
    By toutarrive in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2010, 17:20
  4. about QAbstractItemDelegate
    By drizzlefsh in forum Qt Programming
    Replies: 4
    Last Post: 11th September 2009, 07:01
  5. Use of QAbstractItemDelegate::closeEditor
    By jml in forum Qt Programming
    Replies: 3
    Last Post: 3rd August 2007, 22:55

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.