?? qabstractitemmodel.cpp
字號:
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{ Q_D(const QAbstractItemModel); if (!d->indexValid(index)) 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. By default, this function will perform a wrapping, string-based comparison on all items, searching for items that begin with the search term specified by \a value.*/QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const{ QModelIndexList result; uint matchType = flags & 0x0F; Qt::CaseSensitivity cs = flags & Qt::MatchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive; 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(); QString t = v.toString(); switch (matchType) { case Qt::MatchRegExp: if (QRegExp(text, cs).exactMatch(t)) result.append(idx); break; case Qt::MatchWildcard: if (QRegExp(text, cs, QRegExp::Wildcard).exactMatch(t)) result.append(idx); break; case Qt::MatchStartsWith: if (t.startsWith(text, cs)) result.append(idx); break; case Qt::MatchEndsWith: if (t.endsWith(text, cs)) result.append(idx); break; case Qt::MatchFixedString: if (t.compare(text, cs) == 0) result.append(idx); break; case Qt::MatchContains: default: if (t.contains(text, cs)) 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{ return QSize(1, 1);}/*! Called to let the model know that it should submit whatever it has cached to the permanent storage. Typically used for row editing. Returns false on error, otherwise true.*/bool QAbstractItemModel::submit(){ return true;}/*! Called to let the model know that it should discart whatever it has cached. Typically used for row editing.*/void QAbstractItemModel::revert(){ // do nothing}/*! Returns the data for the given \a role and \a section in the header with the specified \a orientation. \sa Qt::ItemDataRole, setHeaderData(), QHeaderView*/QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const{ Q_UNUSED(orientation); if (role == Qt::DisplayRole) return section + 1; if (role == Qt::TextAlignmentRole) return Qt::AlignCenter; return QVariant();}/*! Sets the \a role for the header \a section to \a value. The \a orientation gives the orientation of the header. Note that the headerDataChanged() signal must be emitted explicitly when reimplementing this function. \sa Qt::ItemDataRole, headerData()*/bool QAbstractItemModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role){ Q_UNUSED(section); Q_UNUSED(orientation); Q_UNUSED(value); Q_UNUSED(role); return false;}/*! \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, void *ptr) const Creates a model index for the given \a row and \a column with the internal pointer \a ptr. This function provides a consistent interface that model subclasses must use to create model indexes.*//*! \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, int id) const \obsolete Use QModelIndex QAbstractItemModel::createIndex(int row, int column, quint32 id) instead.*//*! \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, quint32 id) const Creates a model index for the given \a row and \a column with the internal identifier, \a id. This function provides a consistent interface that model subclasses must use to create model indexes.*//*! \internal*/void QAbstractItemModel::encodeData(const QModelIndexList &indexes, QDataStream &stream) const{ QModelIndexList::ConstIterator it = indexes.begin(); for (; it != indexes.end(); ++it) stream << (*it).row() << (*it).column() << itemData(*it);}/*! \internal */bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &parent, QDataStream &stream){ int top = INT_MAX; int left = INT_MAX; int bottom = 0; int right = 0; QVector<int> rows, columns; QVector<QMap<int, QVariant> > data; while (!stream.atEnd()) { int r, c; QMap<int, QVariant> v; stream >> r >> c >> v; rows.append(r); columns.append(c); data.append(v); top = qMin(r, top); left = qMin(c, left); bottom = qMax(r, bottom); right = qMax(c, right); } // insert the dragged items into the table, use a bit array to avoid overwriting items, // since items from different tables can have the same row and column int dragRowCount = 0; int dragColumnCount = right - left + 1; // Compute the number of continuous rows upon insertion and modify the rows to match QVector<int> rowsToInsert(bottom + 1); for (int i = 0; i < rows.count(); ++i) rowsToInsert[rows.at(i)] = 1; for (int i = 0; i < rowsToInsert.count(); ++i) { if (rowsToInsert[i] == 1){ rowsToInsert[i] = dragRowCount; ++dragRowCount; } } for (int i = 0; i < rows.count(); ++i) rows[i] = top + rowsToInsert[rows[i]]; QBitArray isWrittenTo(dragRowCount * dragColumnCount); // make space in the table for the dropped data int colCount = columnCount(parent); if (colCount == 0) { insertColumns(colCount, dragColumnCount - colCount, parent); colCount = columnCount(parent); } insertRows(row, dragRowCount, parent); row = qMax(0, row); column = qMax(0, column); // set the data in the table for (int j = 0; j < data.size(); ++j) { int relativeRow = rows.at(j) - top; int relativeColumn = columns.at(j) - left; int destinationRow = relativeRow + row; int destinationColumn = relativeColumn + column; int flat = (relativeRow * dragColumnCount) + relativeColumn; // if the item was already written to, or we just can't fit it in the table, create a new row if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) { destinationColumn = qBound(column, destinationColumn, colCount - 1); destinationRow = row + dragRowCount; insertRows(row + dragRowCount, 1, parent); flat = (dragRowCount * dragColumnCount) + relativeColumn; isWrittenTo.resize(++dragRowCount * dragColumnCount); } if (!isWrittenTo.testBit(flat)) { QModelIndex idx = index(destinationRow, destinationColumn, parent); setItemData(idx, data.at(j)); isWrittenTo.setBit(flat); } } return true;}/*! Begins a row insertion operation. When reimplementing insertRows() in a subclass, you must call this function \e before inserting data into the model's underlying data store. The \a parent index corresponds to the parent into which the new rows are inserted; \a first and \a last are the row numbers that the new rows will have after they have been inserted. \table 80% \row \o \inlineimage modelview-begin-insert-rows.png Inserting rows \o Specify the first and last row numbers for the span of rows you want to insert into an item in a model. For example, as shown in the diagram, we insert three rows before row 2, so \a first is 2 and \a last is 4: \code beginInsertRows(parent, 2, 4); \endcode This inserts the three new rows as rows 2, 3, and 4. \row \o \inlineimage modelview-begin-append-rows.png Appending rows \o To append rows, insert them after the last row. For example, as shown in the diagram, we append two rows to a collection of 4 existing rows (ending in row 3), so \a first is 4 and \a last is 5: \code beginInsertRows(parent, 4, 5); \endcode This appends the two new rows as rows 4 and 5. \endtable \sa endInsertRows()*/void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last){ Q_ASSERT(first >= 0); Q_ASSERT(last >= first); Q_D(QAbstractItemModel); d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); emit rowsAboutToBeInserted(parent, first, last); d->rowsAboutToBeInserted(parent, first, last);}/*! Ends a row insertion operation. When reimplementing insertRows() in a subclass, you must call this function \e after inserting data into the model's underlying data store. \sa beginInsertRows()*/void QAbstractItemModel::endInsertRows(){ Q_D(QAbstractItemModel); QAbstractItemModelPrivate::Change change = d->changes.pop(); d->ro
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -