Important: Please read the Qt Code of Conduct -

How to I use filterAcceptsRow() function ?

  • Don't give me url. Only view sample code with filterAcceptsRow().


    It is a URL but if you read it under the sub heading "Filtering" you will see a code example.

  • I just want to appear in the folder I want. (QTreeview) How can I do?

  • Here is a sample from something I'm working on. It accepts only rows where the string _search is contained in one of the specified columns.


    class StatusFilter : public QSortFilterProxyModel
        explicit StatusFilter(QObject * parent = 0);
        ~StatusFilter()= default;
        void setSearch(const QString &str) noexcept;
        QString _search;
        bool filterAcceptsRow(int source_row, const QModelIndex &) const override;


    bool StatusFilter::filterAcceptsRow(int source_row, const QModelIndex &) const
        QStringList sl= _search.split(QRegExp("[ ,]+"),QString::SkipEmptyParts);
        for (auto const &s:sl)
            bool found= false;
            for (int col:
                QModelIndex ix= sourceModel()->index(source_row,col);
                QString str=;
                if (str.contains(s))
                    return true;
        return false;
    void StatusFilter::setSearch(const QString &str) noexcept
        _search= str.toLower();

  • @tako said:

    I just want to appear in the folder I want with qtreeview . This sample code How will it help me?

  • @tako
    Your question lacks detail. If you're having a problem understanding how filterAcceptsRow() works, please supply something you've tried and the problem you're having with it.

  • @mjsurette ok knk

Log in to reply