QSortFilterProxyModel - strange filtering behavior



  • When I set the filter it seems to use a LIKE logic, whether I use fixed strings or RegExpr as a filter.
    I have attached some code that demonstrates this:
    I add the numbers from 0 to 30 to a standard model (as strings).
    I am trying to get only the row that has the value "5", but instead I get every row that has a 5 in it (5,15,25).
    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"

    #include <QStandardItemModel>
    #include <QSortFilterProxyModel>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QStandardItemModel mdl;
    for (int i = 0; i < 30;i++) {
    mdl.appendRow(new QStandardItem( QString::number(i) ));
    }
    qDebug() << "Items in model : " << mdl.rowCount();
    QSortFilterProxyModel proxyMdl;
    proxyMdl.setSourceModel(&mdl);
    qDebug() << "Items in proxy (no filter): " << proxyMdl.rowCount();
    proxyMdl.setFilterKeyColumn(0);
    proxyMdl.setFilterFixedString("5");
    qDebug() << "Items in proxy (fixed filter string): " << proxyMdl.rowCount();
    proxyMdl.setFilterRegExp(QRegExp("5", Qt::CaseInsensitive, QRegExp::FixedString));
    proxyMdl.invalidate();
    qDebug() << "Items in proxy (reqExpr filter): " << proxyMdl.rowCount();

    return a.exec&#40;&#41;;
    

    }
    @
    The output of the above is strangely
    @
    Items in model : 30
    Items in proxy (no filter): 30
    Items in proxy (fixed filter string): 3
    Items in proxy (reqExpr filter): 3
    @



  • Hi hsfougaris,

    it does exactkly what you tell him :-)

    you set a regexp with "5", which matches all strings that contain a 5 somewhere.
    If it should be exactly five, use start and end delimiter:

    @
    proxyMdl.setFilterRegExp(QRegExp("^5$", Qt::CaseInsensitive, QRegExp::FixedString));
    @

    I think setFilterFixedString does something similar...



  • Your code doesn't return anything.

    In order for it to work, it needs to be specified as
    @
    proxyMdl.setFilterRegExp(QRegExp("^5$", Qt::CaseInsensitive, QRegExp::RegExp));
    @
    which makes sense.

    So what's the point of FixedString, in both setFilterReqExp and setFilterFixedString, if they are not fixed strings?



  • It seems that a FixedString as understood by QRegExpr is not what I would expect.

    From the documentation:
    @
    QRegExp::FixedString means that the pattern is interpreted as a plain string. Special characters (e.g., backslash) don't need to be escaped then.
    @
    which I guess means I will always have to prepend ^ and append $ - there is no way for the QSFPM (I hope I got that right Gerolf) to do a straight comparison out of the box.



  • [quote author="harry" date="1302186916"]It seems that a FixedString as understood by QRegExpr is not what I would expect.
    [/quote]

    I agree with that. I would assume a plain text match would be an exact match. This is unclear, and should be reported as a bug. If it is not considered a bug in the code, it is a bug in the documentation in that it is not clear what this method does.



  • I agree, but since I don't think I had much luck with my reporting of the other bug ("QTBUG-18578":http://bugreports.qt.nokia.com/browse/QTBUG-18578) about setFilter [they still want more info for something that is plainly obvious], maybe I'll let someone else report this one...

    [EDIT: fixed link, Volker]



  • The way Jira is handed by the Trolls is a sore point indeed with many community members. I am not even allowed to vote...

    Still, the request for a compilable is possible. Just have the app generate a small sqlite database with a single table with one column with some data. However, I think your code sample is clear enough.



  • Well if they insist some more that's what I'll end up doing, but like you said it's not the best handling of a case: it feels like they're trying to give you a run around....


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.