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.