PDA

View Full Version : QAbstractItemModel with two QSortFilterProxyModels



graffy
27th December 2013, 15:56
Hello,

I'm trying to access data in a QAbstractItemModel implementation that is fitlered through two successive QSortFitlerProxyModel instances.

The data is stored with two identifiers "section" and "name". I instance a "section" filter to filter the model items first by their section, then a "name" filter to choose an item by it's particular name.

The problem is, I can't seem to access the original data through the name filter.

The data filters fine, and the correct columns are being mapped to the source model, but trying to access the data from the second-level filter results in an invalid index at the source level (row() and column() == -1).



void buldAndTestModel (QAbstractItemModel *source, const QString &sectionName, const QString &name)
{
QSortFilterProxyModel *sectionModel = new QSortFilterProxyModel();
sectionModel->setSourceModel(source);
sectionModel->setFilterRegExp(sectionName);
sectionModel->setFilterKeyColumn(1);

QSortFilterProxyModel *nameModel = new QSortFilterProxyModel();
nameModel->setSourceModel(sectionModel);
nameModel->setFilterRegExp(name);
nameModel->setFilterKeyColumn(0);

QStringList valueList = nameModel->data(nameModel->index(0, 2, QModelIndeX())).toStringList();

//valueList is empty and the call to the source->data method reveals index.row() and index.column() == -1
}

anda_skoa
27th December 2013, 16:42
What about the index returned from nameModel->index(0, 2, QModelIndex())?
Is nameModel->rowCount() > 0? Is nameModel->columnCount() >= 3?

Cheers,
_

graffy
27th December 2013, 18:18
Rats. columnCount() was causing the problem. It was reporting 2, changed to 3 and it's good. Thanks.