Results 1 to 10 of 10

Thread: FilterProxy model

  1. #1
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question FilterProxy model

    Hi guys,
    I have a question about filtering the TableView.
    I need to make something like this:
    I have a TableView widget that has a model set. model has 4 columns and views selection behaviour is set to rows...
    I need to implement a filtering of a view according to some value specified in lets say line edits that will be placed at the top(or better above) of that view...And filter should be applied when a checkbox( that is near those line edits) is checked...When it is unchecked it should not be applied and view should return to its previous state -> to see all of model's items.
    Ive read about QSortFilterProxyModel...but found a sentence in doc saying:
    "The QSortFilterProxyModel class provides support for sorting and filtering data passed between another model and a view"
    Do I understand it correctly that there Is no way of filtering the view of ONE model using this FilterProxyModel class ?
    How would you advise me to implement my widget ?
    Thanks a Lot!

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

    Default Re: FilterProxy model

    Of course there is. You pass the original model to the proxy and then pass the proxy model instead of the original model to the view. If you want complex filtering you'll have to reimplement filterAcceptRow().

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

    gyre (3rd December 2007)

  4. #3
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: FilterProxy model

    another question...
    is it possible to filter to filter the view (using the SortFilterProxyModel) using 2 line edits ?...custom filter proxy example shows only using one line edit for sorting so Im just asking if it is possible to filter view using two ciretrias given by two line edits..

  5. #4
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: FilterProxy model

    Yes, its possible.
    In fact, when you look at the code of the Custom Sort/Filter Model example, you see that filterAcceptsRow actually implements 3 different criterias.

    So yes, you can do so, that the row is shown only if the data in column1 fits the criterion from lineEdit1 and/or the data in column2 fits the criterion in lineEdit2.

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

    Default Re: FilterProxy model

    If you reimplement filterAcceptRows you can do any filtering you want. You get an index of the model, you do whatever you want with it and then return true or false to make the index visible or not. For example once I made a filter that filtered rows based on results of some heavy calculations performed on the whole model. The threshold value was set by a slider that was connected to the proxy model so whenever the slider value changed, the filter was recalculated.

  7. #6
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: FilterProxy model

    thanks guys...Ill try to implement it as you advised me...
    just one last question...
    Is there such thing(method) that would allow me to disable/enable filtering upon some event ? like when the state of checkbox changes ...

    I thought I understood invalidateFilter() method but it seems I dont...what does it do ?
    Thanks

  8. #7
    Join Date
    Nov 2007
    Posts
    51
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: FilterProxy model

    Since you are using a custom proxy model, you probably dont use the setFilterRegExp, setFilterFixedString, etc. methods, to pass a single string as filtering criterion.
    The above functions (if i understood correctly) do call invalidateFilter themselves, when they are called.
    So you have to use invalidateFilter yourself after setting the new filtering criterias; you are basically forcing the model to consider your updated criterias instead the old ones.

    You can enable/disable using the filter, if thats what you meant by that, by for instance setting a flag in your model, when the checkbox is checked, then in filterAcceptsRow if this flag is checked, you use the filter, if not, you just return true, thus every row is accepted.
    Don't forget calling invalidateFilter after you set the flag.

    Hope it helps.

  9. #8
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: FilterProxy model

    I dont know...I tried to implement the model as show in example but it somehow doesnt work...Can anyone tell me why not ? ...
    I attached the header and source files Im using...
    Thanks
    Attached Files Attached Files
    Last edited by gyre; 4th December 2007 at 14:02. Reason: updated contents

  10. #9
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: FilterProxy model

    I changed the files to ones that are in attachements...
    Filtering now works OK...but when I remove all characters from lineedit from which I control filtering the model does not get to the original state and ALL items are filtered...dont know why
    Attached Files Attached Files

  11. #10
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: FilterProxy model

    I have found a problem...it has to be checked whether the regexps are empty...
    see the attached code if youre interested...
    Attached Files Attached Files

Similar Threads

  1. Sharing Selections between Model and ProxyModel
    By mentat in forum Qt Programming
    Replies: 14
    Last Post: 27th January 2010, 17:31
  2. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  3. Coin3d + Qt: SIGLNALs and SLOTs
    By vonCZ in forum Newbie
    Replies: 26
    Last Post: 15th May 2009, 07:34
  4. Help with Model View
    By weepdoo in forum Qt Programming
    Replies: 13
    Last Post: 12th October 2007, 10:32
  5. Model Choices review/questions
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2007, 21: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
  •  
Qt is a trademark of The Qt Company.