📄 qabstractitemmodel.cpp
字号:
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 << "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, 0 is returned rather than a serialized empty list.*/QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const{ if (indexes.count() <= 0) return 0; QMimeData *data = new QMimeData(); QString format = mimeTypes().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. 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. \sa supportedDropActions()*/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) return false; // check if the format is supported QString format = mimeTypes().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);}/*! Returns the drop actions supported by this model. The default implementation returns Qt::CopyAction. It is only necessary to reimplement this function in subclasses if you wish to support more types of drag and drop operation. \sa Qt::DropActions*/Qt::DropActions QAbstractItemModel::supportedDropActions() const{ return Qt::CopyAction;}/*! On models that support this, inserts \a count rows into the model before the given \a row. The items in the new row will be children of the item represented by the \a parent model index. If \a row is 0, the rows are prepended to any existing rows in the parent. If \a row is rowCount(), the rows are appended to any existing rows in the parent. If \a parent has no children, a single column with \a count rows is inserted. Returns true if the rows were successfully inserted; otherwise returns false. The base class implementation does nothing and returns false. If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide you own API for altering the data. \sa insertColumns(), removeRows(), beginInsertRows(), endInsertRows()*/bool QAbstractItemModel::insertRows(int, int, const QModelIndex &){ return false;}/*! On models that support this, inserts \a count new columns into the model before the given \a column. The items in each new column will be children of the item represented by the \a parent model index. If \a column is 0, the columns are prepended to any existing columns. If \a column is columnCount(), the columns are appended to any existing columns. If \a parent has no children, a single row with \a count columns is inserted. Returns true if the columns were successfully inserted; otherwise returns false. The base class implementation does nothing and returns false. If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide you own API for altering the data. \sa insertRows(), removeColumns(), beginInsertColumns(), endInsertColumns()*/bool QAbstractItemModel::insertColumns(int, int, const QModelIndex &){ return false;}/*! On models that support this, removes \a count rows starting with the given \a row under parent \a parent from the model. Returns true if the rows were successfully removed; otherwise returns false. The base class implementation does nothing and returns false. If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide you own API for altering the data. \sa removeRow(), removeColumns(), insertColumns(), beginRemoveRows(), endRemoveRows()*/bool QAbstractItemModel::removeRows(int, int, const QModelIndex &){ return false;}/*! On models that support this, removes \a count columns starting with the given \a column under parent \a parent from the model. Returns true if the columns were successfully removed; otherwise returns false. The base class implementation does nothing and returns false. If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide you own API for altering the data. \sa removeColumn(), removeRows(), insertColumns(), beginRemoveColumns(), endRemoveColumns()*/bool QAbstractItemModel::removeColumns(int, int, const QModelIndex &){ return false;}/*! Fetches any available data for the items with the parent specified by the \a parent index. Reimplement this if you have incremental data. The default implementation does nothing. \sa canFetchMore()*/void QAbstractItemModel::fetchMore(const QModelIndex &){ // do nothing}/*! Returns true if there is more data available for \a parent, otherwise false. The default implementation always returns false. \sa fetchMore()*/bool QAbstractItemModel::canFetchMore(const QModelIndex &) const{ return false;}/*! Returns the item flags for the given \a index. The base class implementation returns a combination of flags that enables the item (\c ItemIsEnabled) and allows it to be selected (\c ItemIsSelectable). \sa Qt::ItemFlags*/Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const{ if (!index.isValid()) return 0; return Qt::ItemIsSelectable|Qt::ItemIsEnabled;}/*! Sorts the model by \a column in the given \a order. The base class implementation does nothing.*/void QAbstractItemModel::sort(int column, Qt::SortOrder order){ Q_UNUSED(column); Q_UNUSED(order); // do nothing}/*! Returns a model index for the buddy of the item represented by \a index. When the user wants to edit an item, the view will call this function to check whether another item in the model should be edited instead, and construct a delegate using the model index returned by the buddy item. In the default implementation each item is its own buddy.*/QModelIndex QAbstractItemModel::buddy(const QModelIndex &index) const{ return index;}/*! Returns a list of indexes for the items where the data stored under the given \a role matches the specified \a value. The way the search is performed is defined by the \a flags given. The list that is returned may be empty. The search starts from the \a start index, and continues until the number of matching data items equals \a hits, the search reaches the last row, or the search reaches \a start again, depending on whether \c MatchWrap is specified in \a flags.*/QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const{ QModelIndexList result; uint matchType = flags & 0x0F; bool caseSensitive = flags & Qt::MatchCaseSensitive; bool recurse = flags & Qt::MatchRecursive; bool wrap = flags & Qt::MatchWrap; bool allHits = (hits == -1); QString text; // only convert to a string if it is needed QModelIndex p = parent(start); int from = start.row(); int to = rowCount(p); // iterates twice if wrapping for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) { for (int r = from; (r < to) && (allHits || result.count() < hits); ++r) { QModelIndex idx = index(r, start.column(), p); if (!idx.isValid()) continue; QVariant v = data(idx, role); // QVariant based matching if (matchType == Qt::MatchExactly) { if (value == v) result.append(idx); } else { // QString based matching if (text.isEmpty()) { // lazy conversion text = value.toString(); if (!caseSensitive) text = text.toLower(); } QString t = v.toString(); if (!caseSensitive) t = t.toLower(); switch (matchType) { case Qt::MatchRegExp: if (QRegExp(text).exactMatch(t)) result.append(idx); break; case Qt::MatchWildcard: if (QRegExp(text, Qt::CaseSensitive, QRegExp::Wildcard).exactMatch(t)) result.append(idx); break; case Qt::MatchStartsWith: if (t.startsWith(text)) result.append(idx); break; case Qt::MatchEndsWith: if (t.endsWith(text)) result.append(idx); break; case Qt::MatchContains: default: if (t.contains(text)) result.append(idx); } } if (recurse && hasChildren(idx)) { // search the hierarchy result += match(index(0, idx.column(), idx), role, (text.isEmpty() ? value : text), (allHits ? -1 : hits - result.count()), flags); } } // prepare for the next iteration from = 0; to = start.row(); } return result;}/*! Returns the row and column span of the item represented by \a index. Note: span is not used currently, but will be in the future.*/QSize QAbstractItemModel::span(const QModelIndex &) const{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -