Search code examples
c++qtqtreeviewqsortfilterproxymodel

Wrong QModelIndex on QTreeView using custom QSortFilterProxyModel


I have a QTreeView based on a QStandardItemModel filled with subclassed QStandardItem. The Item-sublcass just has two more datapointers and do not effect the item in any other way, so PTSModelItem behaves like QStandardItem.

I am using a custom QSortFilterProxyModel by subclassing filterAcceptsRow() to enable filtering on the custom datapointers.

The treeView is shown correct, until i enable the filter. Then the List element is never filtered out and the child-elements are missing completly on the list (not child matches) or completly shown (at least one child matches).

The problem i encounter, the generated index in filterAcceptsRow() is always the List Element (Child of rootItem) even with the source_row increasing.

Model with parameter values for filterAcceptsRow(int source_row, QModelIndex source_parent):

RootItem (invisible)
|-List1            
| |-Child1         
| |-Child2
| |-Child3
|-List2
| |-Child1
| |-Child4

The problem is, indepentend from source row, calling

QModelIndex itemIndex = sourceModel()->index(source_row,i,source_parent);
if(!itemIndex.isValid())
     continue;
PTSModelItem* item = static_cast<PTSModelItem*>(itemIndex.internalPointer());

The item returned is alway List1 or List2, even with source_row being 0,1,2.

Filling the model (listitem just holds text and a QList of "childText":

QStandardItemModel *model = new QStandardItemModel();
QStandardItem *rootItem = model->invisibleRootItem();
foreach(PTSItem* listItem, modelItemList)
{
    PTSModelItem *item = new PTSModelItem(tr("List: %1").arg(listItem->getListNumber()),listItem,0,PTSModelItem::ITEM_TYPE_LISTNUMBER);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    rootItem->appendRow(item);

    PTSModelItem *identTitle = new PTSModelItem("",listItem,0,PTSModelItem::ITEM_TYPE_IDENTEMPTY);
    identTitle->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    rootItem->setChild(item->row(),1,identTitle);

    PTSModelItem *readDateItem = new PTSModelItem(tDateString,listItem,0,PTSModelItem::ITEM_TYPE_CREATED);
    readDateItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    rootItem->setChild(item->row(),2,readDateItem);

    PTSModelItem *writeDateItem = new PTSModelItem(tDateString,listItem,0,PTSModelItem::ITEM_TYPE_LASTSET);
    writeDateItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    rootItem->setChild(item->row(),3,writeDateItem);

    for(int i=0; i<listItem->size();i++)
    {
        QList<QStandardItem*> childItems;

        PTSModelItem *toolItem = new PTSModelItem(tr("T%1").arg(listItem->getToolNumber(i)),listItem,i,PTSModelItem::ITEM_TYPE_TOOLNUMBER);
        toolItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
        childItems.append(toolItem);

        PTSModelItem *identItem = new PTSModelItem(listItem->getIdentNumber(i),listItem,i,PTSModelItem::ITEM_TYPE_IDENTNUMBER);
        identItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
        childItems.append(identItem);

        PTSModelItem *readDate = new PTSModelItem("",listItem,i,PTSModelItem::ITEM_TYPE_CREATEDVALID);
        readDate->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
        childItems.append(readDate);

        PTSModelItem *writeDate = new PTSModelItem("",listItem,i,PTSModelItem::ITEM_TYPE_LASTSETVALID);
        writeDate->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
        childItems.append(writeDate);

        item->appendRow(childItems);
    }
}

Filter implementation:

bool PTSFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{

    QModelIndex itemIndex = sourceModel()->index(source_row,0,source_parent);
    if(!itemIndex.isValid())
        return false;
    PTSModelItem* item = static_cast<PTSModelItem*>(itemIndex.internalPointer());

    QString text = item->text();              // THIS always return ListN
    if(item->type() == QStandardItem::Type)
    {
        if(item == static_cast<QStandardItemModel*>(sourceModel())->invisibleRootItem())
            return true;
    }

    //Custom filtering starts here but breaks, since item is always only the List-element
}

Solution

  • QStandardItemModel requires you to not use the internalPointer() method but

    QStandardItemModel::itemFromIndex()

    So problem solved by changing item retrieval in filterAcceptsRow to:

    PTSModelItem* item = static_cast<PTSModelItem*>(static_cast<QStandardItemModel*>(sourceModel())->itemFromIndex(usedIndex));