Results 1 to 2 of 2

Thread: Filter tree view

  1. #1
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    509
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Exclamation Filter tree view

    Hi, I have a QTreeView with the following structure:
    Qt Code:
    1. -1
    2. |-A
    3. | |-a
    4. | |-b
    5. |
    6. |-B
    7. | |-a
    8. |
    9. |-C
    10. |-b
    To copy to clipboard, switch view to plain text mode 
    I would like to filter this and show only the nodes that contain a "a" item, like this:
    Qt Code:
    1. -1
    2. |-A
    3. | |-a
    4. |
    5. |-B
    6. |-a
    To copy to clipboard, switch view to plain text mode 
    but using a custom QSortFilterProxyModel I only achieved this:
    Qt Code:
    1. -1
    2. |-A
    3. | |-a
    4. |
    5. |-B
    6. | |-a
    7. |
    8. |-C
    To copy to clipboard, switch view to plain text mode 
    which is understandable since my filter condition is applied to the a,b,c items only.
    I also tried to filter the filtered output with a second QSortFilterProxyModel, but that did not help since the hasChildren() method for the model indexes always returned 0 and all items got filtered out...

    Is there a simple way to completely filter out the nodes that do not contain an "a" item (the "C" node in the example above)?

    Thanks,

    Ginsengelf

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Filter tree view

    I faced the same problem few month ago, I solve it by using the folowing class :

    songtreeproxyfilter.h :
    Qt Code:
    1. #ifndef SONGTREEPROXYFILTER_H
    2. #define SONGTREEPROXYFILTER_H
    3.  
    4. #include <QSortFilterProxyModel>
    5.  
    6. class SongTreeProxyFilter : public QSortFilterProxyModel
    7. {
    8. public:
    9. SongTreeProxyFilter(QObject *parent = NULL);
    10. bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
    11. private :
    12. bool hasToBeDisplayed(const QModelIndex index) const;
    13. };
    14.  
    15. #endif // SONGTREEPROXYFILTER_H
    To copy to clipboard, switch view to plain text mode 

    songtreeproxyfilet.c
    Qt Code:
    1. #include "songtreeproxyfilter.h"
    2.  
    3. #include <QtDebug>
    4.  
    5. SongTreeProxyFilter::SongTreeProxyFilter(QObject *parent):
    6. {
    7. }
    8.  
    9. bool SongTreeProxyFilter::filterAcceptsRow(int sourceRow,
    10. const QModelIndex &sourceParent) const
    11. {
    12. QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
    13.  
    14. return hasToBeDisplayed(index);
    15. }
    16.  
    17. bool SongTreeProxyFilter::hasToBeDisplayed(const QModelIndex index) const
    18. {
    19. bool result = false;
    20. // How many child this element have
    21. if ( sourceModel()->rowCount(index) > 0 )
    22. {
    23. for( int ii = 0; ii < sourceModel()->rowCount(index); ii++)
    24. {
    25. QModelIndex childIndex = sourceModel()->index(ii,0,index);
    26. if ( ! childIndex.isValid() )
    27. break;
    28. result = hasToBeDisplayed(childIndex);
    29. if (result)
    30. {
    31. // there is atless one element to display
    32. break;
    33. }
    34. }
    35. }
    36. else
    37. {
    38. QModelIndex useIndex = sourceModel()->index(index.row(), 1, index.parent());
    39. QString type = sourceModel()->data(useIndex, Qt::DisplayRole).toString();
    40. if ( ! type.contains(filterRegExp()))
    41. {
    42. result = false;
    43. }
    44. else
    45. {
    46. result = true;
    47. }
    48. }
    49. return result;
    50. }
    To copy to clipboard, switch view to plain text mode 

    I hope it can help.

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

    Ginsengelf (20th October 2011)

Similar Threads

  1. Replies: 0
    Last Post: 5th September 2011, 08:08
  2. Replies: 19
    Last Post: 25th November 2010, 08:52
  3. Set a filter on a model/view
    By graciano in forum Qt Programming
    Replies: 6
    Last Post: 13th June 2010, 17:07
  4. Optimizing filterAcceptsRow() to filter a tree
    By vfernandez in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2007, 12:50
  5. Filter Proxy Model to Autoupdate View
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2006, 20:32

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.