📄 qabstractitemmodel.cpp
字号:
return dbg; Q_UNUSED(idx);#endif}QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx){ if (idx.d) dbg << idx.d->index; else dbg << QModelIndex(); return dbg;}#endifvoid QAbstractItemModelPrivate::removePersistentIndexData(QPersistentModelIndexData *data){ int data_index = persistent.indexes.indexOf(data); persistent.indexes.removeAt(data_index); Q_ASSERT(!persistent.indexes.contains(data)); // update the references to moved persistend indexes for (int i = persistent.moved.count() - 1; i >= 0; --i) { QList<int> moved = persistent.moved.at(i); for (int j = moved.count() - 1; j >= 0; --j) { if (moved.at(j) > data_index) --persistent.moved[i][j]; else if (moved.at(j) == data_index) persistent.moved[i].removeAll(j); } } // update the references to invalidated persistend indexes for (int i = persistent.invalidated.count() - 1; i >= 0; --i) { QList<int> invalidated = persistent.invalidated.at(i); for (int j = invalidated.count() - 1; j >= 0; --j) { if (invalidated.at(j) > data_index) --persistent.invalidated[i][j]; else if (invalidated.at(j) == data_index) persistent.invalidated[i].removeAll(j); } }}void QAbstractItemModelPrivate::invalidate(int position){ // no need to make invalidate recursive, since the *AboutToBeRemoved functions // will register indexes to be invalidated recursively persistent.indexes[position]->index = QModelIndex();}void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent, int first, int last){ Q_UNUSED(last); QList<int> persistent_moved; for (int position = 0; position < persistent.indexes.count(); ++position) { QModelIndex index = persistent.indexes.at(position)->index; if (index.isValid() && index.parent() == parent && index.row() >= first) persistent_moved.append(position); } persistent.moved.push(persistent_moved);}void QAbstractItemModelPrivate::rowsInserted(const QModelIndex &parent, int first, int last){ QList<int> persistent_moved = persistent.moved.pop(); int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested for (int i = 0; i < persistent_moved.count(); ++i) { int position = persistent_moved.at(i); QModelIndex old = persistent.indexes.at(position)->index; persistent.indexes[position]->index = q_func()->index(old.row() + count, old.column(), parent); }}void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last){ QList<int> persistent_moved; QList<int> persistent_invalidated; // find the persistent indexes that are affected by the change, either by being in the removed subtree // or by being on the same level and below the removed rows for (int position = 0; position < persistent.indexes.count(); ++position) { bool level_changed = false; QModelIndex current = persistent.indexes.at(position)->index; while (current.isValid()) { QModelIndex current_parent = current.parent(); if (current_parent == parent) { // on the same level as the change if (!level_changed && current.row() > last) // below the removed rows persistent_moved.append(position); else if (current.row() <= last && current.row() >= first) // in the removed subtree persistent_invalidated.append(position); break; } current = current_parent; level_changed = true; } } persistent.moved.push(persistent_moved); persistent.invalidated.push(persistent_invalidated);}void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent, int first, int last){ QList<int> persistent_moved = persistent.moved.pop(); // it is important that we update the persistent index positions first and then invalidate indexes later // this is because the invalidation of indexes may remove them from the list of persistent indexes // and this in turn will go through the list of moved and invalidated indexes and update them int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested for (int i = 0; i < persistent_moved.count(); ++i) { int position = persistent_moved.at(i); QModelIndex old = persistent.indexes.at(position)->index; persistent.indexes[position]->index = q_func()->index(old.row() - count, old.column(), parent); } QList<int> persistent_invalidated = persistent.invalidated.pop(); for (int j = 0; j < persistent_invalidated.count(); ++j) invalidate(persistent_invalidated.at(j));}void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &parent, int first, int last){ Q_UNUSED(last); QList<int> persistent_moved; for (int position = 0; position < persistent.indexes.count(); ++position) { QModelIndex index = persistent.indexes.at(position)->index; if (index.isValid() && index.parent() == parent && index.column() >= first) persistent_moved.append(position); } persistent.moved.push(persistent_moved);}void QAbstractItemModelPrivate::columnsInserted(const QModelIndex &parent, int first, int last){ QList<int> persistent_moved = persistent.moved.pop(); int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested for (int i = 0; i < persistent_moved.count(); ++i) { int position = persistent_moved.at(i); QModelIndex old = persistent.indexes.at(position)->index; persistent.indexes[position]->index = q_func()->index(old.row(), old.column() + count, parent); }}void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last){ QList<int> persistent_moved; QList<int> persistent_invalidated; // find the persistent indexes that are affected by the change, either by being in the removed subtree // or by being on the same level and to the right of the removed columns for (int position = 0; position < persistent.indexes.count(); ++position) { bool level_changed = false; QModelIndex current = persistent.indexes.at(position)->index; while (current.isValid()) { QModelIndex current_parent = current.parent(); if (current_parent == parent) { // on the same level as the change if (!level_changed && current.column() > last) // right of the removed columns persistent_moved.append(position); else if (current.column() <= last && current.column() >= first) // in the removed subtree persistent_invalidated.append(position); break; } current = current_parent; level_changed = true; } } persistent.moved.push(persistent_moved); persistent.invalidated.push(persistent_invalidated);}void QAbstractItemModelPrivate::columnsRemoved(const QModelIndex &parent, int first, int last){ QList<int> persistent_moved = persistent.moved.pop(); // it is important that we update the persistent index positions first and then invalidate indexes later // this is because the invalidation of indexes may remove them from the list of persistent indexes // and this in turn will go through the list of moved and invalidated indexes and update them int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested for (int i = 0; i < persistent_moved.count(); ++i) { int position = persistent_moved.at(i); QModelIndex old = persistent.indexes.at(position)->index; persistent.indexes[position]->index = q_func()->index(old.row(), old.column() - count, parent); } QList<int> persistent_invalidated = persistent.invalidated.pop(); for (int j = 0; j < persistent_invalidated.count(); ++j) invalidate(persistent_invalidated.at(j));}void QAbstractItemModelPrivate::reset(){ for (int i = 0; i < persistent.indexes.count(); ++i) persistent.indexes[i]->index = QModelIndex();}/*! \class QModelIndex qabstractitemmodel.h \brief The QModelIndex class is used to locate data in a data model. \ingroup model-view \mainclass This class is used as an index into item models derived from QAbstractItemModel. The index is used by item views, delegates, and selection models to locate an item in the model. New QModelIndex objects are created by the model using the QAbstractItemModel::createIndex() function. An \e invalid model index can be constructed with the QModelIndex constructor. Invalid indexes are often used as parent indexes when referring to top-level items in a model. Model indexes refer to items in models, and contain all the information required to specify their locations in those models. Each index is located in a given row and column, and may have a parent index; use row(), column(), and parent() to obtain this information. Each top-level item in a model is represented by a model index that does not have a parent index - in this case, parent() will return an invalid model index, equivalent to an index constructed with the zero argument form of the QModelIndex() constructor. To obtain a model index that refers to an existing item in a model, call QAbstractItemModel::index() with the required row and column values, and the model index of the parent. When referring to top-level items in a model, supply QModelIndex() as the parent index. The model() function returns the model that the index references as a QAbstractItemModel. The child() function is used to examine the items held beneath the index in the model. The sibling() function allows you to traverse items in the model on the same level as the index. Model indexes can become invalid over time so they should be used immediately and then discarded. If you need to keep a model index over time use a QPersistentModelIndex. \sa \link model-view-programming.html Model/View Programming\endlink QPersistentModelIndex QAbstractItemModel*//*! \fn QModelIndex::QModelIndex() Creates a new empty model index. This type of model index is used to indicate that the position in the model is invalid. \sa isValid() QAbstractItemModel*//*! \fn QModelIndex::QModelIndex(int row, int column, void *data, const QAbstractItemModel *model) \internal Creates a new model index at the given \a row and \a column, pointing to some \a data.*//*! \fn QModelIndex::QModelIndex(const QModelIndex &other) Creates a new model index that is a copy of the \a other model index.*//*! \fn QModelIndex::~QModelIndex() Destroys the model index.*//*! \fn int QModelIndex::row() const Returns the row this model index refers to.*//*! \fn int QModelIndex::column() const Returns the column this model index refers to.*//*! \fn void *QModelIndex::internalPointer() const Returns a \c{void} \c{*} pointer used by the model to associate the index with the internal data structure. \sa QAbstractItemModel::createIndex()*//*! \fn void *QModelIndex::internalId() const Returns a \c{qint64} used by the model to associate the index with the internal data structure. \sa QAbstractItemModel::createIndex()*//*! \fn bool QModelIndex::isValid() const Returns true if this model index is valid; otherwise returns false. A valid index belongs to a model, and has non-negative row and column numbers. \sa model(), row(), column()*//*! \fn const QAbstractItemModel *QModelIndex::model() const Returns a pointer to the model containing the item that this index refers to.*//*! \fn QModelIndex QModelIndex::sibling(int row, int column) const Returns the sibling at \a row and \a column or an invalid QModelIndex if there is no sibling at this position. \sa parent() child()*//*! \fn QModelIndex QModelIndex::child(int row, int column) const Returns the child of the model index that is stored in the given \a row and \a column. \sa parent() sibling()*//*! \fn QVariant QModelIndex::data(int role) const Returns the data for the given \a role for the item referred to by the index.*//*! \fn bool QModelIndex::operator==(const QModelIndex &other) const Returns true if this model index refers to the same location as the \a other model index; otherwise returns false. Note that all values in the model index are used when comparing with another model index.*//*! \fn bool QModelIndex::operator!=(const QModelIndex &other) const Returns true if this model index does not refer to the same location as the \a other model index; otherwise returns false.*//*! \fn QModelIndex QModelIndex::parent() const Return the parent of the model index, or QModelIndex() if it has no parent. \sa child() sibling() model()*//*! \class QAbstractItemModel qabstractitemmodel.h \brief The QAbstractItemModel class provides the abstract interface for item model classes. \ingroup model-view \mainclass The QAbstractItemModel class defines the standard interface that item models must use to be able to interoperate with other components in the model/view architecture. It is not supposed to be instantiated directly. Instead, you should subclass it to create new models. The QAbstractItemModel class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. If you need a model to use with a QListView or a QTableView, you
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -