#include <QtGui>#include "typeFilter.h"void addMail(QAbstractItemModel *model, const QString id, const QPixmap Pix, const QDateTime &date, const QString &info){model->setData(model->index(0,0),id, 3); //идентификатор, 3 роль ToolTipRole(если прописать тут 0, // то фильтрует, но тогда id отображается на форме)model->setData(model->index(0,0),Pix,1); //иконкаmodel->setData(model->index(0,1),date); //времяmodel->setData(model->index(0,2),info); //текст сообщения}QAbstractItemModel *createMailModel (QObject *parent){QStandardItemModel *model2 = new StandardItemModel (0,3, parent);QPixmap *pixCrash = new QPixmap ("crash.png");QPixmap *pixAtt = new QPixmap ("att.png");addMail (model2, "1", *pixCrash, QDateTime(QDate(2013,12,23),QTime(17,03)), QObject::tr("Сообщение"));addMail (model2, "2", *pixAtt, QDateTime(QDate(2013,12,23),QTime(17,03)), QObject::tr("Сообщение"));return model2;}int main (int argc, char *argv[]){QApplication app(argc,argv);Window window;window.setSourceModel (createMailModel (&window));window.show();return app.exec();}
#include <QtGui>#include "mysortfilterproxymodel.h"MySortFilterProxyModel::MySortFilterProxyModel (QObject *parent) :QSortFilterProxyModel (parent){}bool MySortFilterProxyModel::filterAcceptRow (int sourceRow, const QModelIndex &sourceParent) const{QModelIndex index0=soutceModel()->index(sourceRow, 0, sourceParent);return (sourceModel()->data(index0).toString.contains(filterRegExp()));}
#include <QtGui>#include "mysortfilterproxymodel.h"#include "typeFilter.h"Window::Window(){proxyModel = new MySortFilterProxyModel(this);proxyModel->setFilterKeyColumn(0);readButton = new QPushButton; proxyView = new QTreeView;proxyView->setRootIsDecorated(false);proxyView->setModel(proxyModel);proxyView->setSortingEnabled(true);proxyView->sortByColumn(0, Qt::AscendingOrder);proxyView->setSelectionBehavior(QAbstractItenView::SelectRows);connect (readButton, SIGNAL(clicked()), this, SLOT (iconFilter()));QHBoxLayout *proxyLayout = new QHBoxLayout;proxyLayout ->addWidget(readButton);proxyLayout ->addWidget(proxyView);setLayout(proxyLayout);}void Window::setSourceModel (QAbstractItemModel *model){proxyModel->setSourceModel(model);}void Window::iconFilter(){proxyModel->setFilterRole(3);proxyModel->setFilterRegExp(QRegExp("1"));}
#ifndef RTY#define RTY#include <QSortFilterProxyModel>#include <QDate>class MySortFilterProxyModel : public QSortFilterProxyModel{Q_OBJECTpublic: MySortFilterProxyModel (QObject *parent = 0);protected:bool filterAcceptRow (int sourceRow, const QModelIndex &sourceParent) const;};#endif
#ifndef RTY2#define RTY2#include <QWidget>#include <QModelIndex>class QAbstractItemModel;class QTreeView;class QPushButton;class MySortFilterProxyModel; class Window : public QWidget{Q_OBJECTpublic:Window();void setSourceModel (QAbstractItemModel *model);private slots:void iconFilter();private:MySortFilterProxyModel *proxyModel;QTreeView *proxyView;QPushButton *readButton;};#endif
C++ (Qt)bool MySortFilterProxyModel::filterAcceptRow (int sourceRow, const QModelIndex &sourceParent) const{QModelIndex index0=soutceModel()->index(sourceRow, 0, sourceParent);return (sourceModel()->data(index0).toString.contains(filterRegExp()));}