📄 qabstractitemmodel.cpp
字号:
Returns the sibling at \a row and \a column for the item at \a index, or an invalid QModelIndex if there is no sibling at that location. sibling() is just a convenience function that finds the item's parent, and uses it to retrieve the index of the child item in the specified \a row and \a column. \sa index(), QModelIndex::row(), QModelIndex::column()*//*! \fn int QAbstractItemModel::rowCount(const QModelIndex &parent) const Returns the number of rows under the given \a parent. When the parent is valid it means that rowCount is returning the number of children of parent. \bold{Tip:} When implementing a table based model, rowCount() should return 0 when the parent is valid. \sa columnCount()*//*! \fn int QAbstractItemModel::columnCount(const QModelIndex &parent) const Returns the number of columns for the children of the given \a parent. When the parent is valid it means that rowCount is returning the number of children of parent. In most subclasses, the number of columns is independent of the \a parent. For example: \quotefromfile itemviews/simpledommodel/dommodel.cpp \skipto ::columnCount \printuntil /^\}/ \bold{Tip:} When implementing a table based model, columnCount() should return 0 when the parent is valid. \sa rowCount()*//*! \fn void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) This signal is emitted whenever the data in an existing item changes. The affected items are those between \a topLeft and \a bottomRight inclusive (of the same parent). Note that this signal must be emitted explicitly when reimplementing the setData() function. \sa headerDataChanged(), setData(), layoutChanged()*//*! \fn void QAbstractItemModel::rowsInserted(const QModelIndex &parent, int start, int end) This signal is emitted after rows have been inserted into the model. The new items are those between \a start and \a end inclusive, under the given \a parent item. \bold{Note:} Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code. \sa insertRows(), beginInsertRows()*//*! \fn void QAbstractItemModel::rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) This signal is emitted just before rows are inserted into the model. The new items will be positioned between \a start and \a end inclusive, under the given \a parent item. \bold{Note:} Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code. \sa insertRows(), beginInsertRows()*//*! \fn void QAbstractItemModel::rowsRemoved(const QModelIndex &parent, int start, int end) This signal is emitted after rows have been removed from the model. The removed items are those between \a start and \a end inclusive, under the given \a parent item. \bold{Note:} Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code. \sa removeRows(), beginRemoveRows()*//*! \fn void QAbstractItemModel::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) This signal is emitted just before rows are removed from the model. The items that will be removed are those between \a start and \a end inclusive, under the given \a parent item. \bold{Note:} Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code. \sa removeRows(), beginRemoveRows()*//*! \fn void QAbstractItemModel::columnsInserted(const QModelIndex &parent, int start, int end) This signal is emitted after columns have been inserted into the model. The new items are those between \a start and \a end inclusive, under the given \a parent item. \bold{Note:} Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code. \sa insertColumns(), beginInsertColumns()*//*! \fn void QAbstractItemModel::columnsAboutToBeInserted(const QModelIndex &parent, int start, int end) This signal is emitted just before columns are inserted into the model. The new items will be positioned between \a start and \a end inclusive, under the given \a parent item. \bold{Note:} Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code. \sa insertColumns(), beginInsertColumns()*//*! \fn void QAbstractItemModel::columnsRemoved(const QModelIndex &parent, int start, int end) This signal is emitted after columns have been removed from the model. The removed items are those between \a start and \a end inclusive, under the given \a parent item. \bold{Note:} Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code. \sa removeColumns(), beginRemoveColumns()*//*! \fn void QAbstractItemModel::columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end) This signal is emitted just before columns are removed from the model. The items to be removed are those between \a start and \a end inclusive, under the given \a parent item. \bold{Note:} Components connected to this signal use it to adapt to changes in the model's dimensions. It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code. \sa removeColumns(), beginRemoveColumns()*//*! Returns true if the model returns a valid QModelIndex for \a row and \a column with \a parent, otherwise returns false.*/bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const{ if (row < 0 || column < 0) return false; return row < rowCount(parent) && column < columnCount(parent);}/*! Returns true if \a parent has any children; otherwise returns false. Use rowCount() on the parent to find out the number of children. \sa parent() index()*/bool QAbstractItemModel::hasChildren(const QModelIndex &parent) const{ return (rowCount(parent) > 0) && (columnCount(parent) > 0);}/*! Returns a map with values for all predefined roles in the model for the item at the given \a index. Reimplemented this function if you want to extend the default behavior of this function to include custom roles in the map. \sa Qt::ItemDataRole, data()*/QMap<int, QVariant> QAbstractItemModel::itemData(const QModelIndex &index) const{ QMap<int, QVariant> roles; for (int i = 0; i < Qt::UserRole; ++i) { QVariant variantData = data(index, i); if (variantData.type() != QVariant::Invalid) roles.insert(i, variantData); } return roles;}/*! Sets the \a role data for the item at \a index to \a value. Returns true if successful; otherwise returns false. The dataChanged() signal should be emitted if the data was successfully set. The base class implementation returns false. This function and data() must be reimplemented for editable models. Note that the dataChanged() signal must be emitted explicitly when reimplementing this function. \sa Qt::ItemDataRole, data(), itemData()*/bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role){ Q_UNUSED(index); Q_UNUSED(value); Q_UNUSED(role); return false;}/*! \fn QVariant QAbstractItemModel::data(const QModelIndex &index, int role) const = 0 Returns the data stored under the given \a role for the item referred to by the \a index. \sa Qt::ItemDataRole, setData(), headerData()*//*! For every Qt::ItemDataRole in \a roles, sets the role data for the item at \a index to the associated value in \a roles. Returns true if successful; otherwise returns false. \sa setData() data() itemData()*/bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles){ bool b = true; for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it) b = b && setData(index, it.value(), it.key()); return b;}/*! Returns a list of MIME types that can be used to describe a list of model indexes. \sa mimeData()*/QStringList QAbstractItemModel::mimeTypes() const{ QStringList types; types << QLatin1String("application/x-qabstractitemmodeldatalist"); return types;}/*! Returns an object that contains serialized items of data corresponding to the list of \a indexes specified. The formats used to describe the encoded data is obtained from the mimeTypes() function. If the list of indexes is empty, or there are no supported MIME types, 0 is returned rather than a serialized empty list. \sa mimeTypes(), dropMimeData()*/QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const{ if (indexes.count() <= 0) return 0; QStringList types = mimeTypes(); if (types.isEmpty()) return 0; QMimeData *data = new QMimeData(); QString format = types.at(0); QByteArray encoded; QDataStream stream(&encoded, QIODevice::WriteOnly); encodeData(indexes, stream); data->setData(format, encoded); return data;}/*! Handles the \a data supplied by a drag and drop operation that ended with the given \a action. Returns true if the data and action can be handled by the model; otherwise returns false. Although the specified \a row, \a column and \a parent indicate the location of an item in the model where the operation ended, it is the responsibility of the view to provide a suitable location for where the data should be inserted. For instance, a drop action on an item in a QTreeView can result in new items either being inserted as children of the item specified by \a row, \a column, and \a parent, or as siblings of the item. When row and column are -1 it means that it is up to the model to decide where to place the data. This can occur in a tree when data is dropped on a parent. Models will usually append the data to the parent in this case. Returns true if the dropping was successful otherwise false. \sa supportedDropActions(), {Using Drag and Drop with Item Views}*/bool QAbstractItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent){ // check if the action is supported if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction)) return false; // check if the format is supported QStringList types = mimeTypes(); if (types.isEmpty()) return false; QString format = types.at(0); if (!data->hasFormat(format)) return false; if (row > rowCount(parent)) row = rowCount(parent); if (row == -1) row = rowCount(parent); if (column == -1) column = 0; // decode and insert QByteArray encoded = data->data(format); QDataStream stream(&encoded, QIODevice::ReadOnly); return decodeData(row, column, parent, stream);}/*! \since 4.2 Returns the drop actions supported by this model. The default implementation returns Qt::CopyAction. Reimplement this function if you wish to support additional actions. Note that you must also reimplement the dropMimeData() function to handle the additional operations. \sa dropMimeData(), Qt::DropActions, {Using Drag and Drop with Item Views}*/Qt::DropActions QAbstractItemModel::supportedDropActions() const{ return Qt::CopyAction;}/*! Returns the actions supported by the data in this model. The default implementation returns supportedDropActions() unless specific values have been set with setSupportedDragActions(). supportedDragActions() is used by QAbstractItemView::startDrag() as the default values when a drag occurs. \sa Qt::DropActions, {Using Drag and Drop with Item Views}*/Qt::DropActions QAbstractItemModel::supportedDragActions() const{ // ### Qt 5: make this virtual or these properties Q_D(const QAbstractItemModel); if (d->supportedDragActions != -1) return d->supportedDragActions; return supportedDropActions();}/*! \since 4.2 Sets the supported drag \a actions for the items in the model. \sa supportedDragActions(), {Using Drag and Drop with Item Views}*/void QAbstractItemModel::setSupportedDragActions(Qt::DropActions actions){ Q_D(QAbstractItemModel);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -