Results 1 to 5 of 5

Thread: Synchronizing selections in filtered and non-filtered model views

  1. #1
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    106
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Synchronizing selections in filtered and non-filtered model views

    Hi there,

    I have created a model that sources from some data source, and a filter proxy that filters the data provided by the model:

    data -> model -> filter_proxy_model

    I also have 3 views, the first two show the filtered data (they use filter_proxy_model) and the last one shows the unfiltered data (uses model directly).

    Now I want to synchronize selections between the three views, and naturally, if I select something in view 3, that is fully or partially filtered out in view 1 and 2, the selection in these views should be reduced accordingly.

    What's the best way to do that? Simply assigning a QItemSelectionModel created for the filtered view to all three views doesn't to the job. It seems that I need some kind of ItemSelectionProxyModel as well...

    What are your recommendations?

    Andreas
    Andreas

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

    Default Re: Synchronizing selections in filtered and non-filtered model views

    Unfortunately there is no simple solution to that. You need to connect to selectionChanged() signal of both (filtered and unfiltered) selection models and update the other selection model accordingly using mapToSource() and mapFromSource() of the proxy model.

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

    ghorwin (20th September 2007)

  4. #3
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    106
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: Synchronizing selections in filtered and non-filtered model views

    Works well, except that because of the filtering, the QItemSelection objects need to be transformed just in a list of model indices disregarding the invalid ones. And then there doesn't seem to be an easy way to combine them back into a QItemSelection to reduce the number of model indices again. So, the performance benefit of using QItemSelection is essentially lost.

    Also, the QItemSelectionModels tend to ping pong their change events so one has to manually disable the event while synchronizing the selections. But after all, it works.
    Andreas

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

    Default Re: Synchronizing selections in filtered and non-filtered model views

    Or you could just check if the new selection you want to apply is different than the old one.

  6. #5
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    106
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Synchronizing selections in filtered and non-filtered model views

    Hmm, this will still give you an unwanted ping-pong. For instance, when you make a drag/range selection in the unfiltered model view, this will create one QItemSelection object. However, when I set the selection in the filtered view, where some of the elements in the range are not present, this will become a list of single QModelIndices that are selected. Now, the filtered ItemSelectionModel pings it back to the unfiltered view but now I get a list of singe model indices vs. the current single QItemSelection model. So, I must assume a new selection was made and pong the filtered version back to the filtered model.

    So, blocking the selection events (just by setting a flag) is working out best and is also faster than comparing a potentially long list of selections.

    Unless someone has a good idea how to avoid this manual selection-synchronizing alltogether?
    Andreas

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.