Results 1 to 9 of 9

Thread: QSortFilterProxyModel problem

  1. #1
    Join Date
    May 2006
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSortFilterProxyModel problem

    Hi,

    I'm wondering how to use QSortFilterProxyModel in the following situation. I have a tree mode laid out like this with 4 columns:

    A 1 2 3
    |_B 1 2 3

    where B is a child item of A with the same number of columns as A. I have two tree views on this model. In the first tree view, I only want to see

    A
    |_ B

    and in the second one I only want to see

    1 2 3
    1 2 3

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

    Default Re: QSortFilterProxyModel problem

    Subclass the model and reimplement filterAcceptsColumn to accept (1) only the first column, (2) all but the first column.

    If you don't want a parent-child relation in the second case, reimplement mapFromSource to return indexes with an invalid parent.

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

    blukske (20th June 2006)

  4. #3
    Join Date
    May 2006
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel problem

    Thanks for your answer, but that does not seem to work. My guess is that when filterAcceptsColumn() returns false for the first column in the second view, the child item B is not considered anymore, and thus its columns are not processed. I end up with:

    first view:

    A
    |_B

    second view

    1 2 3

    so the columns from B are lost. From the manual with regard to filterAcceptsColumn():

    For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown.

    Any ideas? Should I implement a complete QAbstractProxyModel for this to work? (Seems like overkill).

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

    Default Re: QSortFilterProxyModel problem

    Reimplement mapFromSource and mapToSource.

  6. #5
    Join Date
    May 2006
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel problem

    I tried that, but I'm not sure how that would help. Where would I have to call these methods? QSortFilterProxyModel does not use these methods internally.

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

    Default Re: QSortFilterProxyModel problem

    Quote Originally Posted by blukske
    I tried that, but I'm not sure how that would help. Where would I have to call these methods?
    You don't. The proxy model will call them to do a mapping from its indexes to source model indexes and the other way round.

    QSortFilterProxyModel does not use these methods internally.
    Have you checked in QAbstractProxyModel too?

  8. #7
    Join Date
    May 2006
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel problem

    Quote Originally Posted by wysota
    You don't. The proxy model will call them to do a mapping from its indexes to source model indexes and the other way round.
    I expected that as well. As it turns out, QSortFilterProxyModel maintains its own internal proxy_to_source & source_to_proxy mappings.

    Quote Originally Posted by wysota
    Have you checked in QAbstractProxyModel too?
    I looked into it, but it requires a lot of additional signal/slot administration. Simply implementing the pure virtual functions is not enough, since changes to the source model are not propagated via the abstract proxy model to the view automatically. But I think I'll have to go that way now, as I'm ready to give up on QSortFilterProxyModel

    My last try is to re-implement index() and data() using mapToSource & mapFromSource and see where that leaves me.

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

    Default Re: QSortFilterProxyModel problem

    Quote Originally Posted by blukske
    I looked into it, but it requires a lot of additional signal/slot administration.
    I meant did you look into its source code while trying to find where mapFromSource and mapToSource are called.

    Don't give up. All you need is to reimplement mapToSource and mapFromSource. And when I think of it, subclassing the abstract class directly could be the way to go, if you don't need the filtering and sorting functionality.

  10. #9
    Join Date
    May 2006
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel problem

    Quote Originally Posted by wysota
    I meant did you look into its source code while trying to find where mapFromSource and mapToSource are called.

    Don't give up. All you need is to reimplement mapToSource and mapFromSource. And when I think of it, subclassing the abstract class directly could be the way to go, if you don't need the filtering and sorting functionality.
    I gave up . I checked QAbstractProxyModel's source, but again, mapFromSource/mapToSource are only called by mapSelectionFromSource/ToSource. Implementing them for either QAbstractProxyModel/QSortFilterProxyModel is not enough, because they are not getting called at all. I've got it working now by simply hiding/unhiding the columns in the tree view. Not the solution I wanted, but it works for now.

    I'll try to implement a proper QAbstractProxyModel later on.

Similar Threads

  1. QDockWidget/Central Widget Problem
    By Lemming in forum Qt Programming
    Replies: 7
    Last Post: 3rd April 2016, 09:52
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.