为什么继承QSortFilterProxyModel时没有调用filterAcceptsRow ?

Why filterAcceptsRow when inheriting from QSortFilterProxyModel is not invoked?

本文关键字:调用 filterAcceptsRow 继承 QSortFilterProxyModel 为什么      更新时间:2023-10-16

从QSortFilterProxyModel继承了一个名为customSortFilterProxyModel的类。一个受保护的功能filterAcceptsRow是override。但是,filterAcceptsRow根本不会被调用。有什么问题吗?谢谢。customSortFilterProxyModel.h

class customSortFilterProxyModel: public QSortFilterProxyModel
        {
           Q_OBJECT
        public:
            customSortFilterProxyModel(QObject *parent);
            ~customSortFilterProxyModel();
        protected:
           virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;   
        };
//customSortFilterProxyModel.cpp
customSortFilterProxyModel::customSortFilterProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}
customSortFilterProxyModel::~customSortFilterProxyModel()
{
}
bool customSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
   return true;
}

使用代理模型测试代码

    QStringListModel *newModel = new QStringListModel;
    QStringList strList;
    strList << "1" << "2" << "3" << "4";
newModel->setStringList(strList);
    customSortFilterProxyModel   *m_customSortFilterProxyModel = new customSortFilterProxyModel(this);
       m_customSortFilterProxyModel->setSourceModel(newModel);

调用这个函数对列进行排序

m_customSortFilterProxyModel ->排序(0);

我强制customSortFilterProxyModel重新加载源模型setSourceModel。它的工作原理。但我不确定这是不是正确的解决方案?