📄 qabstractitemmodel.cpp
字号:
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; else if (role == Qt::TextAlignmentRole) return Qt::AlignVCenter; 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 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 = bottom - top + 1 ; int dragColumnCount = right - left + 1; 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 of the new rows to be inserted. \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->rowsInserted(change.parent, change.first, change.last); emit rowsInserted(change.parent, change.first, change.last);}/*! Begins a row removal operation. When reimplementing removeRows() in a subclass, you must call this function \e before removing data from the model's underlying data store. The \a parent index corresponds to the parent from which the new rows are removed; \a first and \a last are the row numbers of the rows to be removed. \sa endRemoveRows()*/void QAbstractItemModel::beginRemoveRows(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 rowsAboutToBeRemoved(parent, first, last); d->rowsAboutToBeRemoved(parent, first, last);}/*! Ends a row removal operation. When reimplementing removeRows() in a subclass, you must call this function \e after removing data from the model's underlying data store. \sa beginRemoveRows()*/void QAbstractItemModel::endRemoveRows(){ Q_D(QAbstractItemModel); QAbstractItemModelPrivate::Change change = d->changes.pop(); d->rowsRemoved(change.parent, change.first, change.last); emit rowsRemoved(change.parent, change.first, change.last);}/*! Begins a column insertion operation. When reimplementing insertColumns() 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 columns are inserted; \a first and \a last are the column numbers of the new columns to be inserted. \sa endInsertColumns()*/void QAbstractItemModel::beginInsertColumns(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 columnsAboutToBeInserted(parent, first, last); d->columnsAboutToBeInserted(parent, first, last);}/*! Ends a column insertion operation. When reimplementing insertColumns() in a subclass, you must call this function \e after inserting data into the model's underlying data store. \sa beginInsertColumns()*/void QAbstractItemModel::endInsertColumns(){ Q_D(QAbstractItemModel); QAbstractItemModelPrivate::Change change = d->changes.pop(); d->columnsInserted(change.parent, change.first, change.last); emit columnsInserted(change.parent, change.first, change.last);}/*! Begins a column removal operation. When reimplementing removeColumns() in a subclass, you must call this function \e before removing data from the model's underlying data store. The \a parent index corresponds to the parent from which the new columns are removed; \a first and \a last are the column numbers of the columns to be removed. \sa endRemoveColumns()*/void QAbstractItemModel::beginRemoveColumns(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 columnsAboutToBeRemoved(parent, first, last); d->columnsAboutToBeRemoved(parent, first, last);}/*! Ends a column removal operation. When reimplementing removeColumns() in a subclass, you must call this function \e after removing data from the model's underlying data store. \sa beginRemoveColumns()*/void QAbstractItemModel::endRemoveColumns(){ Q_D(QAbstractItemModel); QAbstractItemModelPrivate::Change change = d->changes.pop(); d->columnsRemoved(change.parent, change.first, change.last); emit columnsRemoved(change.parent, change.first, change.last);}/*! Resets the model to its original state in any attached views. When a model is reset it means that any previous data reported from the model is now invalid and has to be queried for again. When a model radically changes its data it can sometimes be easier to just call this function rather than emit dataChanged() to inform other components when the underlying data source, or its structure, has changed. \sa modelReset()*/void QAbstractItemModel::reset(){ Q_D(QAbstractItemModel); emit modelReset(); d->reset();}/*! Changes the QPersistentModelIndex that is equal to the given \a from model index to the given \a to model index. If no persistent model index equal to the given \a from model index was found, nothing is changed.*/void QAbstractItemModel::changePersistentIndex(const QModelIndex &from, const QModelIndex &to){ // ### optimize (use QMap ?) Q_D(QAbstractItemModel); QList<QPersistentModelIndexData*> persistentIndexes = d->persistent.indexes; for (int i = 0; i < persistentIndexes.count(); ++i) { if (persistentIndexes.at(i)->index == from) { persistentIndexes.at(i)->index = to; break; } }}/*! \since 4.1 Changes the QPersistentModelIndexes that is equal to the indexes in the given \a from model index list to the given \a to model index list. If no persistent model indexes equal to the indexes in the given \a from model index list was found, nothing is changed.*/void QAbstractItemModel::changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to){ // ### optimize (use QMap ?) Q_D(QAbstractItemModel); QList<QPersistentModelIndexData*> persistentIndexes = d->persistent.indexes; QBitArray changed(persistentIndexes.count()); for (int i = 0; i < from.count(); ++i) { for (int j = 0; j < persistentIndexes.count(); ++j) { if (!changed.at(j) && persistentIndexes.at(j)->index == from.at(i)) { persistentIndexes.at(j)->index = to.at(i); changed.setBit(j); break; } } }}/*! \class QAbstractTableModel \brief The QAbstractTableModel class provides an abstract model that can be subclassed to create table models. \ingroup model-view QAbstractTableModel provides a standard interface for models that represent their data as a two-dimensional array of items. It is not used directly, but must be subclassed.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -