April 30, 2012

Prashant Moglaikar Prashant Moglaikar
Lab Rat
36 posts

Facing difficulties to use share QAbstractItemModel between QTreeView & QListView

 

Hello,

I have QAbstractItemModel implementation which share in two view [ QTreeView & QListView]

List view contains some filters like… All Type, Type 1, Type 2, Type 3. those types represented as parent item in Tree View.
So when i click on Type 1 from Tree View it will generate index click and set root index of list view by them. so it will display all child’s items from Type 1 will be listed in List View. Its works fine for me but,

When i want to use of combination of filters e.g. like Type 1 & Type 2, so in my list view it should display the list of all child’s of Type 1, & Type 2 but it could not be possible because i can set only one root index at a time to list view. So how can i achieve this ?

And same scenario above is applicable to All Type filter as well, how could i display all child’s of Tree view item to be listed in list view.

Any help will be really appreciated.

Thanks
Prashant Moglaikar

3 replies

April 30, 2012

Gerolf Gerolf
Robot Herder
3253 posts

Hi,

you have to craete a proxy model that does all that. The proxy must contain the mappijng between teh list views model index and the original one.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

April 30, 2012

Prashant Moglaikar Prashant Moglaikar
Lab Rat
36 posts

Hi Gerolf,

Thanks for reply i have implementation of proxy model but could figure it out how to map list view model index to get my expected out put could you please give me some hint or any small example so i can get understand how its be implement ?

  1. bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const
  2. {
  3.  QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
  4.  QString checkStr = sourceModel()->data(index, Qt::DisplayRole).toString();
  5.  return ShowThis(index);
  6. }
  7.  
  8. bool MySortFilterProxyModel::ShowThis(const QModelIndex index) const
  9. {
  10.  bool retVal = false;
  11.  //Gives you the info for number of childs with a parent
  12.  if ( sourceModel()->rowCount(index) > 0 )
  13.  {
  14.   for( int nChild = 0; nChild < sourceModel()->rowCount(index); nChild++)
  15.   {
  16.    QModelIndex childIndex = sourceModel()->index(nChild,0,index);
  17.    if ( ! childIndex.isValid() )
  18.     break;
  19.    retVal = ShowThis(childIndex);
  20.    if (retVal)
  21.    {
  22.     break;
  23.    }
  24.   }
  25.  }
  26.  else
  27.  {
  28.   QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
  29.   QString type = sourceModel()->data(useIndex, Qt::DisplayRole).toString();
  30.   //qDebug()<<"type is "<<type;
  31.   if ( ! type.contains(filterRegExp()))
  32.   {
  33.    retVal = false;
  34.   }
  35.   else
  36.   {
  37.    retVal = true;
  38.   }
  39.  }
  40.  return retVal;
  41. }

Thanks
Prashant

April 30, 2012

Prashant Moglaikar Prashant Moglaikar
Lab Rat
36 posts

I use this code currently for the filtering purpose emit signal from line edit as text changed and hide and show items in list view .

 
  ‹‹ SetTimeZoneInfo unable to read the timezone name for the next time      QT_SOURCE_TREE not defined ››

You must log in to post a reply. Not a member yet? Register here!