Results 1 to 4 of 4

Thread: repetetive filtering via QSortFilterProxyModel

  1. #1
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default repetetive filtering via QSortFilterProxyModel

    hello guys,

    hope u can help:
    i have an application that filters data from an underlying table-model via QSortFilterProxyModel and displays results within another table.

    i want some filter-functionality that enables me to extend the given filter-pattern to be a list of filter-patterns which should successively be applied.

    for example lets say my source table contains data entries (rows)

    1. hello world
    2. buenas dias qtcentre
    3. thnx 4 ur help

    my filter patterns are
    1. hello
    2. help

    the resulting list (table) should now contain the rows
    1. hello world
    3. thnx 4 ur help

    i was thinking about putting the patterns into a list and apply each of them successively to the source-table and keep in mind which rows of my source-table succeeded filtering previously for returning appropriate return value for

    Qt Code:
    1. bool QSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
    To copy to clipboard, switch view to plain text mode 

    i dont know if this is the right way to do that.

    (another different annoying thing is that QSortFilterProxyModel has no signal that tells that filtering is done ?)

    any hints and ideas are welcome.

    thnx alot.

  2. #2
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: repetetive filtering via QSortFilterProxyModel

    ...
    has anybody an idea on this (see above)?
    please help.

    besides that:
    i decided to do the filtering stuff within a thread.
    my QSortFilterProxyModel lives within my MainWindow
    and the routine which is running in the thread manages
    a pointer to this proxymodel.
    but whenever i call filterProxyModel->setFilterRegExp(rexp) my
    app crashes.

    i assume this is because the proxymodel lives in a different thread !?!?
    besides getting some opinion from you on doing repetetive filtering requests, i
    would also appreciate to get a hint if i am right by assuming the mentioned crashing
    is caused by using proxyModel within different threads.

    thnx alot.

  3. #3
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: repetetive filtering via QSortFilterProxyModel

    Quote Originally Posted by kerim View Post
    i was thinking about putting the patterns into a list and apply each of them successively to the source-table and keep in mind which rows of my source-table succeeded filtering previously for returning appropriate return value for
    Qt Code:
    1. bool QSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
    To copy to clipboard, switch view to plain text mode 
    Yes, you're right in the track. All you really need to do is to reimplement the filterAcceptsRow() and inside it get the source index:
    Qt Code:
    1. QModelIndex sourceIndex = sourceModel()->index( sourceRow, 0, sourceParent );
    To copy to clipboard, switch view to plain text mode 
    Get string value from the index:
    Qt Code:
    1. QString value = sourceIndex.data().toString();
    To copy to clipboard, switch view to plain text mode 
    And finally, check the value against each pattern:
    Qt Code:
    1. foreach( const QString &pattern, patterns ) {
    2. if( value.contains( pattern ) ) return true;
    3. }
    4. return false;
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by kerim View Post
    (another different annoying thing is that QSortFilterProxyModel has no signal that tells that filtering is done ?)
    Yes it does, rowsRemoved() and rowsInserted() signals are emitted when ever the proxy model changes because of the filtering.


    Added after 21 minutes:


    Quote Originally Posted by kerim View Post
    i assume this is because the proxymodel lives in a different thread !?!?
    besides getting some opinion from you on doing repetetive filtering requests, i
    would also appreciate to get a hint if i am right by assuming the mentioned crashing
    is caused by using proxyModel within different threads.
    Yes, your are right.

    Remember that most of Qt's classes are not thread-safe, this includes models and proxies.
    Secondly, do remember that views can only live in main thread, because the painting must be done there.

    Now, if you modify the model (or proxy model) from another thread the change will most likely trigger the view (which is connected to your model) to update itself.
    When the view is being repainted it will read the underlying model. At that time you have two threads reading and changing the model at same time and there is nothing to protect against it, no mutexes or anything.

    If you are going to use models in another thread then make sure that no other thread can access it. And don't attach a view to that model (or any proxy) either.
    Last edited by joyer83; 24th May 2011 at 18:01.

  4. #4
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: repetetive filtering via QSortFilterProxyModel

    thnx alot man.
    that helped,
    greets.

Similar Threads

  1. Event filtering
    By Cruz in forum Qt Programming
    Replies: 6
    Last Post: 28th March 2011, 13:35
  2. QSortFilterProxyModel not filtering properly
    By freemind in forum Qt Programming
    Replies: 9
    Last Post: 8th August 2010, 02:23
  3. Key filtering
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2010, 10:10
  4. Filtering QSqlQueryModel
    By mourad in forum Qt Programming
    Replies: 4
    Last Post: 2nd August 2009, 10:32
  5. Replies: 1
    Last Post: 10th January 2008, 15:38

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.