Results 1 to 5 of 5

Thread: Model/view, apply a filter on model

  1. #1
    Join Date
    Dec 2010
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Model/view, apply a filter on model

    Hello,

    I am subclassing QSqlTableModel for a model used by a QListView.
    This model represents data stored in a SQLite database.
    In this database I have a flag field (let say an int field at column 0).
    I want my model to present data depending on the flag value, in other word I want to apply a filter on my model.
    So I have a public method setFilter(int flag) to set the current filter on flag value, and I want my model to present only the data (rows) that match this value.

    Let say my db is like:
    flag | value
    0 | foo
    1 | bar
    After setFilter(0) I want my model to present only:
    flag | value
    0 | foo
    The code :
    Qt Code:
    1. MyModel::MyModel(QObject *parent) :
    2. QSqlTableModel(parent),
    3. {
    4. setTable("myTable");
    5. select();
    6. }
    7.  
    8. void
    9. MyModel::setFilter(int flag)
    10. {
    11. currentFlag = flag; // currentFlag is MyModel class member
    12. }
    13.  
    14. MyModel::data(const QModelIndex & index, int role ) const
    15. {
    16. if( index.model()->data(index.sibling(index.row(),0)) == currentFlag) // if the current index points to a row with flag column equals to the current filter, return the value, but this leads to infinite recursive call
    17. return QSqlTableModel::data(index, role);
    18. else return QVariant(); // else return an invalid data
    19. }
    To copy to clipboard, switch view to plain text mode 

    Problem is, to check the flag value I have to call data() inside the data() method, which leads to infinite recursive call.
    How can I solve this ? How can I check value of another ModelIndex in the QAbstractItemModel::data() method ? Is it possible or am I using Model/view classes the wrong way ?

    Hope everything is clear.

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

    Default Re: Model/view, apply a filter on model

    Hi, you can try a proxy model (like QSortfilterProxyModel). The proxy model will check the flag, the normal model underneath will provide the data.

    Ginsengelf
    Last edited by Ginsengelf; 4th February 2011 at 11:39. Reason: reformatted to look better

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/view, apply a filter on model

    Use QSqlTableModel::setFilter().

    For example

    Qt Code:
    1. void
    2. MyModel::setFilter(int flag)
    3. {
    4. QString filterString (QString ("flag=%1").arg(flag));
    5. this->setFilter (filterString);
    6. select();
    7. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Dec 2010
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Model/view, apply a filter on model

    Thanks ! both work !

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/view, apply a filter on model

    The solution of Ginsengelf it's good and it's more general than mine.

    When you use an HUGE Database, probably, my solution is faster because the sorting/filtering phase it's optimized in SQL engines.
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 19
    Last Post: 25th November 2010, 09:52
  2. Set a filter on a model/view
    By graciano in forum Qt Programming
    Replies: 6
    Last Post: 13th June 2010, 18:07
  3. Replies: 0
    Last Post: 21st April 2010, 13:23
  4. Replies: 1
    Last Post: 1st February 2010, 19:42
  5. Filter Proxy Model to Autoupdate View
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2006, 21:32

Tags for this Thread

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.