class MyFilterProxyModel : public QSortFilterProxyModel{ Q_OBJECTpublic: MyFilterProxyModel(QObject *parent = 0);protected: bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const;};
#include <QtGui>#include "mysortfilterproxymodel.h"MyFilterProxyModel::MyFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent){}bool MyFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const{ QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent); return (sourceModel()->data(index1).toString().contains(filterRegExp()));}
\\\\\\\\\\\\\\\\\\\\\ model = new QSqlTableModel(this); proxy = new MyFilterProxyModel(this); proxy->setDynamicSortFilter(true); proxy->setSourceModel(model); ui->tableView->setModel(proxy);
QModelIndex index0 = sourceModel()->index(sourceRow, 1, sourceParent);QModelIndex index1 = sourceModel()->index(sourceRow, 2, sourceParent);return (sourceModel()->data(index0).toString().contains(filterRegExp()) || sourceModel()->data(index1).toString().contains(filterRegExp()));
bool MyFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex sourceParent) const { return true;}
qDebug() << model->rowCount() << " rows loaded in model";
0 rows loaded in model
C++ (Qt)model->select();model->setHeaderData(fio, Qt::Horizontal, tr("Ф.И.О."));model->setHeaderData(otdel, Qt::Horizontal, tr("Отдел"));model->setHeaderData(tel, Qt::Horizontal, tr("Телефон"));