Results 1 to 5 of 5

Thread: QSortProxyModel isnt notified when the sourceModel dies

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSortProxyModel isnt notified when the sourceModel dies

    Hey,

    I have a QSortProxyModel that leeches off another model that I set with setSourceModel().

    When the sourceModel is destroyed, but the QSortProxyModel is still attached to the GUI, it ends up showing ugly empty rows (see screenshot). At this point, sourceModel() method returns NULL and my overriden rowCount() in the QSortProxyModel also returns 0.

    But it looks like this isnt enough, I guess this should trigger a reset_model signal, which it seems isnt done automatically when the sourceModel dies.

    Am I expected to monitor the lifetime of the sourceMOdel on my own? Seems unnecessary, and I already take care of rowCount().
    Attached Images Attached Images

  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: QSortProxyModel isnt notified when the sourceModel dies

    I am guessing that QSortProxyModel (QSPM) listens for the source model's destroyed() signal, so it sets the sourceModel() pointer to null. I have found that QSPM doesn't emit signals that I would expect when the model changes (like dataChanged()), but does emit layoutChanged(). Have you checked for that?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSortProxyModel isnt notified when the sourceModel dies

    You are right, QSPM seems to emit surprisingly few signals. It seems when we have a complex setup involving several nested QSPM and other models, we are expected to smoothen things over ourselves. Qt is only smart enough to keep things in a non-crashing state, inserting grey, empty rows when it hits an invalid state.

    Thanks for the answer.

  4. #4
    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: QSortProxyModel isnt notified when the sourceModel dies

    I use QSPM extensively to create customized scatter plots (with QCustomPlot) by using them to filter a master table model so only the rows I am interested in get passed to the plot. The plot selects two columns from the filtered table to use as the x- and y- coordinates on the dots.

    I have found several gotchas with the stock QSPM:

    - setFilterFixedString() doesn't do an exact match; it matches any string that contains the filter string somewhere in it. If you filter on "ABC", it matches "ABCD", "DABC", and any other string with "ABC" in it. I had to use reg exp filtering with "^ABC$".

    - you *must* call QSPM::invalidate() after changing the filter string / reg exp or nothing happens

    - the only signal I can see coming from QSPM appears to be layoutChanged(), so that's what I use to update my plots. Don't know why the designers made that choice. I would have chosen modelReset() myself since by changing the filter you are potentially changing the model completely, not just its "layout", whatever that is.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    tuli (26th May 2018)

  6. #5
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortProxyModel isnt notified when the sourceModel dies

    That you have to call invalidate() is clearly documented and calling beginResetModel() / endResetModel() on a filter change is really not what you want since in this case the view is completely rebuilding it's internal state which also resets the header sizes, visibility and much more - you won't have this on a simple filter change ...

Similar Threads

  1. Replies: 0
    Last Post: 17th March 2010, 13:17
  2. Replies: 5
    Last Post: 14th May 2009, 10:06
  3. My QThread dies
    By Althor in forum Qt Programming
    Replies: 1
    Last Post: 10th November 2008, 15:08
  4. QList<char*> dies
    By thomaspu in forum Qt Programming
    Replies: 11
    Last Post: 15th March 2008, 23:47
  5. Detect when a TCP Server dies
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 00:40

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.