Qt工作笔记-自定义QSortFilterProxyModel实现多列分别过滤

程序运行截图如下:

原理,重写QSortFilterProxyModel类中的filterAcceptsRow即可:

源码如下:

mysortfilterproxymodel.h

#ifndef MYSORTFILTERPROXYMODEL_H
#define MYSORTFILTERPROXYMODEL_H

#include 
#include 

class MySortFilterProxyModel : public QSortFilterProxyModel
{
    Q_OBJECT
public:
   MySortFilterProxyModel(QObject *parent = 0);
   void setRxCol1(const QString rx);
   void setRxCol2(const QString rx);

protected:
   bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const Q_DECL_OVERRIDE;

private:
   QString m_rxCol1;
   QString m_rxCol2;

};

#endif // MYSORTFILTERPROXYMODEL_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

QT_BEGIN_NAMESPACE
class QStandardItemModel;
class QSortFilterProxyModel;
QT_END_NAMESPACE

class MySortFilterProxyModel;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected slots:
    void col1LineEditChanged(const QString text);
    void col2LineEidtChanged(const QString text);

protected:
    void intsertModel(const int row, const int col, const QString data);

private:
    Ui::Widget *ui;
    QStandardItemModel *m_model;
    MySortFilterProxyModel *m_filterModel;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

mysortfilterproxymodel.cpp

#include "mysortfilterproxymodel.h"
#include 
#include 

MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent)
{
    m_rxCol1 = "";
    m_rxCol2 = "";
}

void MySortFilterProxyModel::setRxCol1(const QString rx)
{
    m_rxCol1 = rx;
}

void MySortFilterProxyModel::setRxCol2(const QString rx)
{
    m_rxCol2 = rx;
}

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);

    return (sourceModel()->data(index0).toString().contains(m_rxCol1)
            && sourceModel()->data(index1).toString().contains(m_rxCol2));
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "mysortfilterproxymodel.h"
#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("CSDN IT1995");
    m_model = new QStandardItemModel;
    m_filterModel = new MySortFilterProxyModel;
    QStringList headList;
    headList << "第一列" << "第二列" << "第三列" << "第四列";
    m_model->setHorizontalHeaderLabels(headList);

    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    for(int row = 0; row < 100; row++){

        for(int col = 0; col < 4; col++){

            intsertModel(row, col, "第" + QString::number(row) + "行,第" + QString::number(col) + "列");
        }
    }

    intsertModel(100, 0, "中文");
    intsertModel(100, 1, "China");
    intsertModel(100, 2, "japan");
    intsertModel(100, 3, "日本人");
    intsertModel(101, 0, "中文");
    intsertModel(101, 1, "东京");
    intsertModel(101, 2, "东京热");
    intsertModel(101, 3, "东京冷");




    connect(ui->colOneLineEdit,SIGNAL(textEdited(QString)), this, SLOT(col1LineEditChanged(QString)));
    connect(ui->colTwoLineEdit,SIGNAL(textEdited(QString)), this, SLOT(col2LineEidtChanged(QString)));

    m_filterModel->setSourceModel(m_model);
    ui->tableView->setModel(m_filterModel);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::col1LineEditChanged(const QString text)
{
    m_filterModel->setRxCol1(text);
    m_filterModel->setSourceModel(m_model);
}

void Widget::col2LineEidtChanged(const QString text)
{
    m_filterModel->setRxCol2(text);
    m_filterModel->setSourceModel(m_model);
}

void Widget::intsertModel(const int row, const int col, const QString data)
{
    QStandardItem *newItem = new QStandardItem(data);
    newItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_model->setItem(row, col, newItem);
}

 

你可能感兴趣的:(C/C++,Qt,工作笔记)