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 -
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.
-
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 ?
@
bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
QString checkStr = sourceModel()->data(index, Qt::DisplayRole).toString();
return ShowThis(index);
}bool MySortFilterProxyModel::ShowThis(const QModelIndex index) const
{
bool retVal = false;
//Gives you the info for number of childs with a parent
if ( sourceModel()->rowCount(index) > 0 )
{
for( int nChild = 0; nChild < sourceModel()->rowCount(index); nChild++)
{
QModelIndex childIndex = sourceModel()->index(nChild,0,index);
if ( ! childIndex.isValid() )
break;
retVal = ShowThis(childIndex);
if (retVal)
{
break;
}
}
}
else
{
QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent());
QString type = sourceModel()->data(useIndex, Qt::DisplayRole).toString();
//qDebug()<<"type is "<<type;
if ( ! type.contains(filterRegExp()))
{
retVal = false;
}
else
{
retVal = true;
}
}
return retVal;
}
@Thanks
Prashant -
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 .