[Qt-interest] Problem with QSortFilterProxyModel

bharath.narayan at nokia.com bharath.narayan at nokia.com
Tue Oct 6 19:26:01 CEST 2009


Hey all,

I am using a QStandarItemModel with 1 column as the source model. I have a QSortFilterProxyModel whose source model is the QStandardItemModel.

I am using this model for a list view which uses data of type Qt::DisplayRole and Qt::DecorationRole.

I have a QLineEdit which I am using to get text to apply filter to the model.

    sourceModel = new QStandardItemModel(0, 1, this);
    sourceModel->insertRow(0);
    sourceModel->setData(sourceModel->index(0, 0), QString("Hello"));
    sourceModel->insertRow(0);
    sourceModel->setData(sourceModel->index(0, 0), QString("Hei"));
    sourceModel->insertRow(0);
    sourceModel->setData(sourceModel->index(0, 0), QString("Hallo"));
    sourceModel->insertRow(0);
    sourceModel->setData(sourceModel->index(0, 0), QString("Ciao"));
    sourceModel->insertRow(0);
    sourceModel->setData(sourceModel->index(0, 0), QString("Hola"));
    sourceModel->insertRow(0);
    sourceModel->setData(sourceModel->index(0, 0), QString("Namaskara"));
    sourceModel->insertRow(0);
    sourceModel->setData(sourceModel->index(0, 0), QString("Vanakkam"));
    sourceModel->insertRow(0);
    QModelIndex mdlIndex = sourceModel->index(0, 0);
    QStringList list;
    list << "one" << "two";
    sourceModel->setData(mdlIndex, list, Qt::DisplayRole);
    sourceModel->setData(mdlIndex, list, Qt::DecorationRole);

    proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(sourceModel);
    // proxyModel->setFilterRole(Qt::DisplayRole | Qt::DecorationRole);

    connect(sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
            this, SLOT(updateProxyModel(QModelIndex,QModelIndex)));
    listView->setModel(proxyModel);

    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical, this);
    layout->addItem(lineEdit);
    layout->addItem(listView);
    setLayout(layout);

I am updating the filter for the proxy model this way:

    QRegExp::PatternSyntax syntax = QRegExp::FixedString;
    Qt::CaseSensitivity cs = Qt::CaseInsensitive;
    QRegExp pattern(lineEdit->text(), cs, syntax);

    proxyModel->setFilterRegExp(pattern);

When I search for `one' |  `two' which I have added above in one of the rows, the list shows empty. I see this problem when the code in bold (commented) is included.
When I comment that line out, all items (except the one which has `one' and `two')  are used for filtering.

Is  there something wrong in the code? Or is this a bug.

Also, I have  certain data which I will set for  a certain role (say Qt::UserRole + 1000) and I would like to filter the model based on this role. Is this possible?

Any help will be appreciated.

Thanks,

Hurraa!!!
/Bharath



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091006/78275858/attachment.html 


More information about the Qt-interest-old mailing list