PDA

View Full Version : QSortProxyModel isnt notified when the sourceModel dies



tuli
18th May 2018, 10:10
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().

d_stranz
18th May 2018, 20:26
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?

tuli
25th May 2018, 16:49
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.

d_stranz
26th May 2018, 17:11
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.

ChristianEhrlicher
1st June 2018, 21:25
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 ...