Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp ----------------------------------------------------
- #include <QApplication>
- #include "widget.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
- // widget.h ---------------------------------------------------
- #ifndef WIDGET_H
- #define WIDGET_H
- #include <QtGui>
- class SortFilterProxyModel : public QSortFilterProxyModel
- {
- public:
- SortFilterProxyModel(QObject *parent = 0)
- : QSortFilterProxyModel(parent)
- {
- }
- bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
- {
- if (filterAcceptsRowItself(source_row, source_parent))
- return true;
- //accept if any of the parents is accepted on it's own merits
- QModelIndex parent = source_parent;
- while (parent.isValid()) {
- if (filterAcceptsRowItself(parent.row(), parent.parent()))
- return true;
- parent = parent.parent();
- }
- //accept if any of the children is accepted on it's own merits
- if (hasAcceptedChildren(source_row, source_parent)) {
- return true;
- }
- return false;
- }
- bool filterAcceptsRowItself(int source_row, const QModelIndex &source_parent) const
- {
- return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
- }
- bool hasAcceptedChildren(int source_row, const QModelIndex &source_parent) const
- {
- QModelIndex item = sourceModel()->index(source_row,0,source_parent);
- if (!item.isValid()) {
- //qDebug() << "item invalid" << source_parent << source_row;
- return false;
- }
- //check if there are children
- int childCount = item.model()->rowCount(item);
- if (childCount == 0)
- return false;
- for (int i = 0; i < childCount; ++i) {
- if (filterAcceptsRowItself(i, item))
- return true;
- //recursive call -> NOTICE that this is depth-first searching, you're probably better off with breadth first search...
- if (hasAcceptedChildren(i, item))
- return true;
- }
- return false;
- }
- };
- class Widget : public QWidget
- {
- Q_OBJECT
- public:
- enum
- {
- SomeRole = Qt::UserRole + 1
- };
- public:
- explicit Widget(QWidget *parent = 0);
- private:
- void adjust_filter();
- void fill_source_model();
- void remove_items();
- private:
- QTreeView *view_;
- SortFilterProxyModel *sort_model_;
- QStandardItemModel *model_;
- QList<QStandardItem*> list_item_;
- };
- #endif // WIDGET_H
- // widget.cpp -----------------------------------------------------
- #include "widget.h"
- Widget::Widget(QWidget *parent)
- : QWidget (parent)
- , view_ (new QTreeView())
- , sort_model_(new SortFilterProxyModel())
- , model_ (new QStandardItemModel())
- {
- sort_model_->setSourceModel(model_);
- sort_model_->setDynamicSortFilter(true);
- view_->setModel(sort_model_);
- QVBoxLayout *vLayout = new QVBoxLayout();
- vLayout->addWidget(view_);
- setLayout(vLayout);
- fill_source_model();
- adjust_filter();
- remove_items();
- }
- void Widget::adjust_filter()
- {
- sort_model_->setFilterKeyColumn(0);
- sort_model_->setFilterRole(SomeRole);
- sort_model_->setFilterRegExp(QRegExp("3000"));
- }
- void Widget::fill_source_model()
- {
- QStandardItem *parent = new QStandardItem("root_item");
- model_->appendRow(parent);
- for(int i = 0; i < 10; ++i)
- {
- const uint some_role_value = 3000 + (qrand() % 3);
- QStandardItem *child = new QStandardItem("first_level id = " + QString::number(i) + "(" + QString::number(some_role_value) + ")");
- child->setData(some_role_value , SomeRole);
- parent->appendRow(child);
- list_item_.append(child);
- for(int j = 0; j < 5; ++j)
- {
- QStandardItem *item = new QStandardItem("second_level id = " + QString::number(j));
- child->insertRow(0, item);
- }
- }
- view_->expand(sort_model_->mapFromSource(model_->indexFromItem(parent)));
- // view_->expandToDepth(0);
- }
- void Widget::remove_items()
- {
- foreach(QStandardItem *item, list_item_)
- {
- QStandardItem *parent = item->parent();
- parent->removeRow(item->row());
- }
- }
- // *.pro file -------------------------------------------------------
- QT += core gui
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- TARGET = test_sort_filter
- TEMPLATE = app
- SOURCES += main.cpp\
- widget.cpp
- HEADERS += widget.h
- FORMS +=
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement