📄 qsortfilterproxymodel.cpp
字号:
: m->proxy_columns.at(start)); int proxy_end = (orientation == Qt::Vertical ? m->proxy_rows.at(end) : m->proxy_columns.at(end)); emit q->headerDataChanged(orientation, proxy_start, proxy_end);}void QSortFilterProxyModelPrivate::_q_sourceReset(){ Q_Q(QSortFilterProxyModel); // All internal structures are deleted in clear() q->reset();}void QSortFilterProxyModelPrivate::_q_sourceLayoutAboutToBeChanged(){ Q_Q(QSortFilterProxyModel); saved_persistent_indexes.clear(); if (persistent.indexes.isEmpty()) return; emit q->layoutAboutToBeChanged(); QModelIndexList source_indexes = store_persistent_indexes(); QModelIndexList::const_iterator it; for(it = source_indexes.constBegin(); it != source_indexes.constEnd(); ++it) saved_persistent_indexes << (*it);}void QSortFilterProxyModelPrivate::_q_sourceLayoutChanged(){ Q_Q(QSortFilterProxyModel); if (saved_persistent_indexes.isEmpty()) { q->invalidate(); return; } QModelIndexList source_indexes; QList<QPersistentModelIndex>::const_iterator it; it = saved_persistent_indexes.constBegin(); for ( ; it != saved_persistent_indexes.constEnd(); ++it) source_indexes << (*it); qDeleteAll(source_index_mapping); source_index_mapping.clear(); update_persistent_indexes(source_indexes); saved_persistent_indexes.clear(); emit q->layoutChanged();}void QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeInserted( const QModelIndex &source_parent, int start, int end){ Q_UNUSED(source_parent); Q_UNUSED(start); Q_UNUSED(end);}void QSortFilterProxyModelPrivate::_q_sourceRowsInserted( const QModelIndex &source_parent, int start, int end){ source_items_inserted(source_parent, start, end, Qt::Vertical);}void QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeRemoved( const QModelIndex &source_parent, int start, int end){ source_items_about_to_be_removed(source_parent, start, end, Qt::Vertical);}void QSortFilterProxyModelPrivate::_q_sourceRowsRemoved( const QModelIndex &source_parent, int start, int end){ source_items_removed(source_parent, start, end, Qt::Vertical);}void QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeInserted( const QModelIndex &source_parent, int start, int end){ Q_UNUSED(source_parent); Q_UNUSED(start); Q_UNUSED(end);}void QSortFilterProxyModelPrivate::_q_sourceColumnsInserted( const QModelIndex &source_parent, int start, int end){ source_items_inserted(source_parent, start, end, Qt::Horizontal);}void QSortFilterProxyModelPrivate::_q_sourceColumnsAboutToBeRemoved( const QModelIndex &source_parent, int start, int end){ source_items_about_to_be_removed(source_parent, start, end, Qt::Horizontal);}void QSortFilterProxyModelPrivate::_q_sourceColumnsRemoved( const QModelIndex &source_parent, int start, int end){ source_items_removed(source_parent, start, end, Qt::Horizontal);}/*! \since 4.1 \class QSortFilterProxyModel \brief The QSortFilterProxyModel class provides support for sorting and filtering data passed between another model and a view. \ingroup model-view QSortFilterProxyModel can be used for sorting items, filtering out items, or both. The model transforms the structure of a source model by mapping the model indexes it supplies to new indexes, corresponding to different locations, for views to use. This approach allows a given source model to be restructured as far as views are concerned without requiring any transformations on the underlying data, and without duplicating the data in memory. Let's assume that we want to sort and filter the items provided by a custom model. The code to set up the model and the view, \e without sorting and filtering, would look like this: \quotefromfile snippets/qsortfilterproxymodel-details/main.cpp \skipto QTreeView \printuntil treeView->setModel(model); To add sorting and filtering support to \c MyItemModel, we need to create a QSortFilterProxyModel, call setSourceModel() with the \c MyItemModel as argument, and install the QSortFilterProxyModel on the view: \quotefromfile snippets/qsortfilterproxymodel-details/main.cpp \skipto QTreeView \printline QTreeView \skipto sourceModel \printuntil treeView->setModel(proxyModel) At this point, neither sorting nor filtering is enabled; the original data is displayed in the view. Any changes made through the QSortFilterProxyModel are applied to the original model. The QSortFilterProxyModel acts as a wrapper for the original model. If you need to convert source \l{QModelIndex}es to sorted/filtered model indexes or vice versa, use mapToSource(), mapFromSource(), mapSelectionToSource(), and mapSelectionFromSource(). \note By default, the model does not dynamically re-sort and re-filter data whenever the original model changes. This behavior can be changed by setting the \l{dynamicSortFilter} property. The \l{itemviews/basicsortfiltermodel}{Basic Sort/Filter Model} and \l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model} examples illustrate how to use QSortFilterProxyModel to perform basic sorting and filtering and how to subclass it to implement custom behavior. \section1 Sorting QTableView and QTreeView have a \l{QTreeView::sortingEnabled}{sortingEnabled} property that controls whether the user can sort the view by clicking the view's horizontal header. For example: \skipto treeView->setSortingEnabled(true) \printline treeView->setSortingEnabled(true) When this feature is on (the default is off), clicking on a header section sorts the items according to that column. By clicking repeatedly, the user can alternate between ascending and descending order. \image qsortfilterproxymodel-sorting.png A sorted QTreeView Behind the scene, the view calls the sort() virtual function on the model to reorder the data in the model. To make your data sortable, you can either implement sort() in your model, or you use a QSortFilterProxyModel to wrap your model -- QSortFilterProxyModel provides a generic sort() reimplementation that operates on the sortRole() (Qt::DisplayRole by default) of the items and that understands several data types, including \c int, QString, and QDateTime. For hierarchical models, sorting is applied recursively to all child items. String comparisons are case sensitive by default; this can be changed by setting the \l sortCaseSensitivity property. Custom sorting behavior is achieved by subclassing QSortFilterProxyModel and reimplementing lessThan(), which is used to compare items. For example: \quotefromfile itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp \skipto ::lessThan \printuntil /^\}/ (This code snippet comes from the \l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model} example.) An alternative approach to sorting is to disable sorting on the view and to impose a certain order to the user. This is done by explicitly calling sort() with the desired column and order as arguments on the QSortFilterProxyModel (or on the original model if it implements sort()). For example: \quotefromfile snippets/qsortfilterproxymodel-details/main.cpp \skipto proxyModel->sort(2, Qt::AscendingOrder) \printline proxyModel->sort(2, Qt::AscendingOrder) \section1 Filtering In addition to sorting, QSortFilterProxyModel can be used to hide items that don't match a certain filter. The filter is specified using a QRegExp object and is applied to the filterRole() (Qt::DisplayRole by default) of each item, for a given column. The QRegExp object can be used to match a regular expression, a wildcard pattern, or a fixed string. For example: \skipto setFilterRegExp \printuntil setFilterKeyColumn For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown. A common use case is to let the user specify the filter regexp, wildcard pattern, or fixed string in a QLineEdit and to connect the \l{QLineEdit::textChanged()}{textChanged()} signal to setFilterRegExp(), setFilterWildcard(), or setFilterFixedString() to reapply the filter. Custom filtering behavior can be achieved by reimplementing the filterAcceptsRow() and filterAcceptsColumn() functions. For example, the following implementation ignores the \l filterKeyColumn property and performs filtering on columns 0, 1, and 2: \quotefromfile itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp \skipto ::filterAcceptsRow \printuntil /^\}/ (This code snippet comes from the \l{itemviews/customsortfiltermodel}{Custom Sort/Filter Model} example.) If you are working with large amounts of filtering and have to invoke invalidateFilter() repeatedly, using reset() may be more efficient, depending on the implementation of your model. However, note that reset() returns the proxy model to its original state, losing selection information, and will cause the proxy model to be repopulated. \section1 Subclassing \bold{Note:} Some general guidelines for subclassing models are available in the \l{Model Subclassing Reference}. Since QAbstractProxyModel and its subclasses are derived from QAbstractItemModel, much of the same advice about subclassing normal models also applies to proxy models. In addition, it is worth noting that many of the default implementations of functions in this class are written so that they call the equivalent functions in the relevant source model. This simple proxying mechanism may need to be overridden for source models with more complex behavior; for example, if the source model provides a custom hasChildren() implementation, you should also provide one in the proxy model. \sa QAbstractProxyModel, QAbstractItemModel, {Model/View Programming}, {Basic Sort/Filter Model Example}, {Custom Sort/Filter Model Example}*//*! Constructs a sorting filter model with the given \a parent.*/QSortFilterProxyModel::QSortFilterProxyModel(QObject *parent) : QAbstractProxyModel(*new QSortFilterProxyModelPrivate, parent){ Q_D(QSortFilterProxyModel); d->sort_column = -1; d->sort_order = Qt::AscendingOrder; d->sort_casesensitivity = Qt::CaseSensitive; d->sort_role = Qt::DisplayRole; d->sort_localeaware = false; d->filter_column = 0; d->filter_role = Qt::DisplayRole; d->dynamic_sortfilter = false; connect(this, SIGNAL(modelReset()), this, SLOT(invalidate()));}/*! Destroys this sorting filter model.*/QSortFilterProxyModel::~QSortFilterProxyModel(){ Q_D(QSortFilterProxyModel); qDeleteAll(d->source_index_mapping); d->source_index_mapping.clear();}/*! \reimp*/void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel){ Q_D(QSortFilterProxyModel); disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex))); disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(_q_sourceRowsInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(_q_sourceLayoutAboutToBeChanged())); disconnect(d->model, SIGNAL(layoutChanged()),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -