Discussion Board

Results 1 to 3 of 3
  1. #1

    Model/view, apply a filter on model


    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 :
    MyModel::MyModel(QObject *parent) :
    MyModel::setFilter(int flag) 
    currentFlag = flag; // currentFlag is MyModel class member
    MyModel::data(const QModelIndex & index, int role ) const
        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 
            return QSqlTableModel::data(index, role);
        else return QVariant(); // else return an invalid data
    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
    Nokia Developer Moderator
    Join Date
    Oct 2009

    Re: Model/view, apply a filter on model

    I believe it's probably easier to use setFilter( const QString & filter )

  3. #3

    Re: Model/view, apply a filter on model

    Thanks, that's what I was looking for, missed it from the doc.
    Another solution I found is to subclass QSortFilterProxyModel.

Similar Threads

  1. Model/View architecture
    By jp4symbian in forum Qt
    Replies: 6
    Last Post: 2010-04-28, 08:34
  2. Confused with Model View Controller...
    By FrankTheFox in forum Symbian
    Replies: 2
    Last Post: 2009-03-24, 13:56
  3. read model no from model.txt
    By rkmohanty in forum Symbian
    Replies: 5
    Last Post: 2008-07-24, 07:34
  4. advice on a MIDlet activation model
    By tomhodder in forum Mobile Java General
    Replies: 1
    Last Post: 2006-08-16, 21:11
  5. Show some text (View Model)
    By wysspeter@bluewin.ch in forum Symbian
    Replies: 2
    Last Post: 2005-08-24, 13:15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts