Advertisement
Guest User

QSortFilterProxyModel

a guest
Jan 24th, 2013
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. // main.cpp ----------------------------------------------------
  2. #include <QApplication>
  3. #include "widget.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication a(argc, argv);
  8.     Widget w;
  9.     w.show();
  10.    
  11.     return a.exec();
  12. }
  13.  
  14. // widget.h ---------------------------------------------------
  15. #ifndef WIDGET_H
  16. #define WIDGET_H
  17.  
  18. #include <QtGui>
  19.  
  20. class SortFilterProxyModel : public QSortFilterProxyModel
  21. {
  22. public:
  23.     SortFilterProxyModel(QObject *parent = 0)
  24.         : QSortFilterProxyModel(parent)
  25.     {
  26.     }
  27.  
  28.     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
  29.     {
  30.         if (filterAcceptsRowItself(source_row, source_parent))
  31.             return true;
  32.  
  33.         //accept if any of the parents is accepted on it's own merits
  34.         QModelIndex parent = source_parent;
  35.         while (parent.isValid()) {
  36.             if (filterAcceptsRowItself(parent.row(), parent.parent()))
  37.                 return true;
  38.             parent = parent.parent();
  39.         }
  40.  
  41.         //accept if any of the children is accepted on it's own merits
  42.         if (hasAcceptedChildren(source_row, source_parent)) {
  43.             return true;
  44.         }
  45.  
  46.         return false;
  47.     }
  48.  
  49.     bool filterAcceptsRowItself(int source_row, const QModelIndex &source_parent) const
  50.     {
  51.         return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
  52.     }
  53.  
  54.     bool hasAcceptedChildren(int source_row, const QModelIndex &source_parent) const
  55.     {
  56.         QModelIndex item = sourceModel()->index(source_row,0,source_parent);
  57.         if (!item.isValid()) {
  58.             //qDebug() << "item invalid" << source_parent << source_row;
  59.             return false;
  60.         }
  61.  
  62.         //check if there are children
  63.         int childCount = item.model()->rowCount(item);
  64.         if (childCount == 0)
  65.             return false;
  66.  
  67.         for (int i = 0; i < childCount; ++i) {
  68.             if (filterAcceptsRowItself(i, item))
  69.                 return true;
  70.             //recursive call -> NOTICE that this is depth-first searching, you're probably better off with breadth first search...
  71.             if (hasAcceptedChildren(i, item))
  72.                 return true;
  73.         }
  74.  
  75.         return false;
  76.     }
  77. };
  78.  
  79.  
  80.  
  81. class Widget : public QWidget
  82. {
  83.     Q_OBJECT
  84.  
  85. public:
  86.     enum
  87.     {
  88.         SomeRole = Qt::UserRole + 1
  89.     };
  90.  
  91. public:
  92.     explicit Widget(QWidget *parent = 0);
  93.    
  94. private:
  95.     void adjust_filter();
  96.     void fill_source_model();
  97.     void remove_items();
  98.  
  99.  
  100. private:
  101.     QTreeView            *view_;
  102.     SortFilterProxyModel *sort_model_;
  103.     QStandardItemModel   *model_;
  104.  
  105.     QList<QStandardItem*> list_item_;
  106. };
  107.  
  108. #endif // WIDGET_H
  109.  
  110. // widget.cpp -----------------------------------------------------
  111. #include "widget.h"
  112.  
  113. Widget::Widget(QWidget *parent)
  114.     : QWidget    (parent)
  115.     , view_      (new QTreeView())
  116.     , sort_model_(new SortFilterProxyModel())
  117.     , model_     (new QStandardItemModel())
  118. {
  119.     sort_model_->setSourceModel(model_);
  120.     sort_model_->setDynamicSortFilter(true);
  121.     view_->setModel(sort_model_);
  122.  
  123.     QVBoxLayout *vLayout = new QVBoxLayout();
  124.     vLayout->addWidget(view_);
  125.     setLayout(vLayout);
  126.  
  127.     fill_source_model();
  128.  
  129.     adjust_filter();
  130.  
  131.     remove_items();
  132. }
  133.  
  134. void Widget::adjust_filter()
  135. {
  136.     sort_model_->setFilterKeyColumn(0);
  137.     sort_model_->setFilterRole(SomeRole);
  138.     sort_model_->setFilterRegExp(QRegExp("3000"));
  139. }
  140.  
  141. void Widget::fill_source_model()
  142. {
  143.     QStandardItem *parent = new QStandardItem("root_item");
  144.     model_->appendRow(parent);
  145.  
  146.     for(int i = 0; i < 10; ++i)
  147.     {
  148.         const uint some_role_value = 3000 + (qrand() % 3);
  149.  
  150.         QStandardItem *child = new QStandardItem("first_level id = " + QString::number(i) + "(" + QString::number(some_role_value) + ")");
  151.         child->setData(some_role_value , SomeRole);
  152.         parent->appendRow(child);
  153.  
  154.         list_item_.append(child);
  155.  
  156.         for(int j = 0; j < 5; ++j)
  157.         {
  158.             QStandardItem *item = new QStandardItem("second_level id = " + QString::number(j));
  159.             child->insertRow(0, item);
  160.         }
  161.     }
  162.  
  163.     view_->expand(sort_model_->mapFromSource(model_->indexFromItem(parent)));
  164. //    view_->expandToDepth(0);
  165. }
  166.  
  167. void Widget::remove_items()
  168. {
  169.     foreach(QStandardItem *item, list_item_)
  170.     {
  171.         QStandardItem *parent = item->parent();
  172.         parent->removeRow(item->row());
  173.     }
  174. }
  175.  
  176. // *.pro file -------------------------------------------------------
  177. QT       += core gui
  178.  
  179. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  180.  
  181. TARGET = test_sort_filter
  182. TEMPLATE = app
  183.  
  184.  
  185. SOURCES += main.cpp\
  186.         widget.cpp
  187.  
  188. HEADERS  += widget.h
  189.  
  190. FORMS    +=
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement