simplify setContents by letting Qt do more of the work
ClosedPublic

Authored by sitter on Jul 18 2017, 10:04 AM.

Details

Summary

This is refactoring the code, it is doing the same as before.

QTableView already manages its own selectionModel based on the model
we give it there does not seem to be a reason why we would duplicate thise code

  • simplify the code managing the models by removing the manual selectionModel management and instead letting Qt handle its creation via QTableView::setModel
  • update the deletion comment to point out that deleting the old model will in fact also delete the associated automatically created selectionModel
  • move behavior and mode setup of the view to the constructor. a search on the QAbstractItemView suggests that neither setting is passed along nor mutated outside the setter, so we only need to set these up once

ideally I suppose instead of changing the model at all we should have a
full model and put a qsortfilterproxymodel in front of it thus making
setContents in its current form obsolete as we'd then only need to filter
as necessary. I am not quite confident enough in my understanding of
how the code presently works to make such an invasive change.

Test Plan

tests still passing

Diff Detail

Repository
R236 KWidgetsAddons
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
sitter created this revision.Jul 18 2017, 10:04 AM
Restricted Application added a project: Frameworks. · View Herald TranscriptJul 18 2017, 10:04 AM
Restricted Application added a subscriber: Frameworks. · View Herald Transcript
davidedmundson requested changes to this revision.Jul 18 2017, 10:40 AM
davidedmundson added a subscriber: davidedmundson.

QAIV creates a new selectionModel whenever we call setModel. (qabstractitemview.cpp:762)

So you're right this is at best, a bit pointlesss, but the fix isn't right, as the code in the new if() will never get called and we won't setSelectionBehavior/setSelectionMode

This revision now requires changes to proceed.Jul 18 2017, 10:40 AM
sitter updated this revision to Diff 16865.Jul 18 2017, 11:42 AM
sitter edited edge metadata.
sitter retitled this revision from do not keep constructing new selectionmodels to simplify setContents by letting Qt do more of the work.
sitter edited the summary of this revision. (Show Details)

did some more digging with input from David.

we can do away with the selectionModel management entirely. QAIV manages the
selectionModel itself via setModel.
it also connects our input model's destroyed() signal to the selection
models deleteLater, so deleting our model will consistently clean up
the selection as well.

additionally the mode and behavior setting affect only the view itself and
are persistent across model changes, so we can move this to the ctor (where
interstingly behavior was already set)

there's one more change we could do to this:
right now we manually connect _k_slotSelectionChanged to the selection model,
we need to do this every time we change model. alternatively we could also
override the protected virtual selectionChanged.
except IIRC this would be BIC with MSVC so we probably don't want this

davidedmundson accepted this revision.Jul 19 2017, 1:35 PM
This revision is now accepted and ready to land.Jul 19 2017, 1:35 PM
cfeck accepted this revision.Aug 30 2017, 2:19 PM

Could you please commit this to land in 5.38?

This revision was automatically updated to reflect the committed changes.