qsortfilterproxymodel.cpp

来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 1,765 行 · 第 1/5 页

CPP
1,765
字号
{    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().    In Qt 4.1, sorting and filtering isn't dynamically reapplied    whenever the data of the original model changes, as an    optimization. Qt 4.2 is expected to offer a mechanism to enable    dynamic sorting and filtering.    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.)    \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->filter_column = 0;    d->filter_role = Qt::DisplayRole;    d->dynamic_sortfilter = false;    connect(this, SIGNAL(modelReset()), this, SLOT(clear()));}/*!    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()),               this, SLOT(_q_sourceLayoutChanged()));    disconnect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset()));    QAbstractProxyModel::setSourceModel(sourceModel);    connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),            this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));    connect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),            this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int)));    connect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),            this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int)));    connect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),            this, SLOT(_q_sourceRowsInserted(QModelIndex,int,int)));    connect(d->model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),            this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int)));    connect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)),            this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int)));    connect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),            this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int)));    connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),            this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int)));    connect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),            this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int)));    connect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),            this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int)));    connect(d->model, SIGNAL(layoutAboutToBeChanged()),            this, SLOT(_q_sourceLayoutAboutToBeChanged()));    connect(d->model, SIGNAL(layoutChanged()),            this, SLOT(_q_sourceLayoutChanged()));    connect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset()));    d->clear_mapping();    reset();}/*!    \reimp*/QModelIndex QSortFilterProxyModel::index(int row, int column, const QModelIndex &parent) const{    Q_D(const QSortFilterProxyModel);    if (row < 0 || column < 0)        return QModelIndex();    QModelIndex source_parent = d->proxy_to_source(parent); // parent is already mapped at this point    IndexMap::const_iterator it = d->create_mapping(source_parent); // but make sure that the children are mapped    if (it.value()->source_rows.count() <= row || it.value()->source_columns.count() <= column)        return QModelIndex();    return d->create_index(row, column, it);}/*!  \reimp*/QModelIndex QSortFilterProxyModel::parent(const QModelIndex &child) const{    Q_D(const QSortFilterProxyModel);    if (!d->indexValid(child))        return QModelIndex();    IndexMap::const_iterator it = d->index_to_iterator(child);    Q_ASSERT(it != d->source_index_mapping.constEnd());    QModelIndex source_parent = it.key();    QModelIndex proxy_parent = d->source_to_proxy(source_parent);    return proxy_parent;}/*!  \reimp*/int QSortFilterProxyModel::rowCount(const QModelIndex &parent) const{    Q_D(const QSortFilterProxyModel);    QModelIndex source_parent = d->proxy_to_source(parent);    IndexMap::const_iterator it = d->create_mapping(source_parent);    return it.value()->source_rows.count();}/*!  \reimp*/int QSortFilterProxyModel::columnCount(const QModelIndex &parent) const{    Q_D(const QSortFilterProxyModel);    QModelIndex source_parent = d->proxy_to_source(parent);    IndexMap::const_iterator it = d->create_mapping(source_parent);    return it.value()->source_columns.count();}/*!  \reimp*/bool QSortFilterProxyModel::hasChildren(const QModelIndex &parent) const{    Q_D(const QSortFilterProxyModel);    QModelIndex source_parent = d->proxy_to_source(parent);    if (!d->model->hasChildren(source_parent))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?