How to I use filterAcceptsRow() function ?



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



  • http://doc.qt.io/qt-5/qsortfilterproxymodel.html#details

    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.

    sample.h

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

    sample.cpp

    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:
                {
                    static_cast<int>(STATUS_COLUMN::RMA_NUMBER),
                    static_cast<int>(STATUS_COLUMN::CUSTOMER_NAME),
                    static_cast<int>(STATUS_COLUMN::LOCATION),
                    static_cast<int>(STATUS_COLUMN::NOTES)
                })
            {
                QModelIndex ix= sourceModel()->index(source_row,col);
                QString str= ix.data().toString().toLower();
                if (str.contains(s))
                    return true;
            }
        }
        return false;
    }
    
    void StatusFilter::setSearch(const QString &str) noexcept
    {
        _search= str.toLower();
        invalidate();
    }
    


  • @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
 

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