c++,不继承QSortFilterProxyModel第一列过滤本月信息,第二列过滤人名叫小明的
时间: 2023-08-14 22:07:18 浏览: 163
你可以通过继承QSortFilterProxyModel并重写filterAcceptsRow函数来实现这个功能。具体步骤如下:
1. 继承QSortFilterProxyModel:
```
class MySortFilterProxyModel : public QSortFilterProxyModel {
Q_OBJECT
public:
explicit MySortFilterProxyModel(QObject *parent = nullptr);
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
};
```
2. 在构造函数中设置过滤规则:
```
MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}
bool MySortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
QModelIndex index0 = sourceModel()->index(source_row, 0, source_parent);
QModelIndex index1 = sourceModel()->index(source_row, 1, source_parent);
QVariant data0 = sourceModel()->data(index0);
QVariant data1 = sourceModel()->data(index1);
if (data0.toString().startsWith(QDate::currentDate().toString("yyyy-MM"))) {
if (data1.toString() == "小明") {
return true;
}
}
return false;
}
```
3. 在使用QTableView时,使用MySortFilterProxyModel代替QSortFilterProxyModel即可。
```
MySortFilterProxyModel *proxyModel = new MySortFilterProxyModel(this);
proxyModel->setSourceModel(model);
tableView->setModel(proxyModel);
```
阅读全文