参考博客:https://blog.csdn.net/qq78442761/article/details/84875123
以下内容为摘抄以上大神的博客:稍微做了点更改
一:界面设计:
二、MySortFilterProxyModel.h文件中:
#ifndef MYSORTFILTERPROXYMODEL_H
#define MYSORTFILTERPROXYMODEL_H
#include <QSortFilterProxyModel>
#include <QRegExp>
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
.cpp文件中如下:
#include "mysortfilterproxymodel.h"
#include <QModelIndex>
#include <QDebug>
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.h文件中如下:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#pragma execution_character_set("utf-8")
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
.cpp文件中如下:
#include "widget.h"
#include "ui_widget.h"
#include "mysortfilterproxymodel.h"
#include <QStandardItemModel>
#include <QRegExp>
#include <QDebug>
#include <QSortFilterProxyModel>
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 << "1" <<"2"<< "3" <<"4";
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, "num:" + QString::number(row) + "hang,num:" + QString::number(col) + "lie");
}
}
intsertModel(100, 0, "111");
intsertModel(100, 1, "China");
intsertModel(100, 2, "japan");
intsertModel(100, 3, "222");
intsertModel(101, 0, "111");
intsertModel(101, 1, "222");
intsertModel(101, 2, "222hot");
intsertModel(101, 3, "222cold");
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);
m_filterModel->setFilterRegExp(text);
}
void Widget::col2LineEidtChanged(const QString text)
{
m_filterModel->setRxCol2(text);
m_filterModel->setSourceModel(m_model);
m_filterModel->setFilterRegExp(text);
}
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);
}
最后直接是main.cpp文件中如下:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
最后实现效果如下: