📄 qstandarditemmodel.cpp
字号:
if (header.size() > section) headerItem = header.at(section); if (headerItem) return headerItem->value(role); return QAbstractItemModel::headerData(section, orientation, role);}/*! \reimp*/bool QStandardItemModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role){ Q_D(QStandardItemModel); if (section < 0 || (orientation == Qt::Horizontal && section >= columnCount()) || (orientation == Qt::Vertical && section >= rowCount())) return false; QStdModelItem *headerItem = 0; QVector<QStdModelItem*> &header = (orientation == Qt::Horizontal ? d->horizontalHeader : d->verticalHeader); role = (role == Qt::EditRole ? Qt::DisplayRole : role); if (header.size() <= section) header.resize(section + 1); headerItem = header[section]; if (!headerItem) { headerItem = new QStdModelItem; header.replace(section, headerItem); } headerItem->setValue(role, value); emit headerDataChanged(orientation, section, section); return true;}/*! Inserts \a count rows into the model, creating new items as children of the given \a parent. The new rows are inserted before the \a row specified. Returns true if the rows were successfully inserted; otherwise returns false. 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 the given \a parent has no children, a single column with \a count rows is inserted. Note that a row with no columns will not show up in the treeview. \sa insertRow(), insertColumns(), removeRows(), rowCount()*/bool QStandardItemModel::insertRows(int row, int count, const QModelIndex &parent){ Q_D(QStandardItemModel); if (count < 1 || row < 0 || row > rowCount(parent) || parent.column() > 0) return false; QVector<QStdModelRow*> &rows = (parent.isValid()) ? d->containedRow(parent, true)->childrenRows : d->topLevelRows; if (!parent.isValid() && d->verticalHeader.size() > row) d->verticalHeader.insert(row, count, 0); beginInsertRows(parent, row, row + count - 1); rows.insert(row, count, 0); endInsertRows(); return true;}/*! Inserts \a count columns into the model, creating new items as children of the given \a parent. The new columns are inserted before the \a column specified. Returns true if the columns were successfully inserted; otherwise returns false. If \a column is 0, the columns are prepended to any existing columns in the parent. If \a column is columnCount(), the columns are appended to any existing columns in the parent. If the given \a parent has no children, a single row with \a count columns is inserted. \sa insertColumn(), insertRows(), removeColumns(), columnCount()*/bool QStandardItemModel::insertColumns(int column, int count, const QModelIndex &parent){ Q_D(QStandardItemModel); if (count < 1 || column < 0 || column > columnCount(parent) || parent.column() > 0) return false; beginInsertColumns(parent, column, column + count - 1); QVector<QStdModelRow*> *rows = &d->topLevelRows; // update the column counters if (parent.isValid()) { QStdModelRow *modelRow = d->containedRow(parent, true); modelRow->childrenColumns += count; rows = &modelRow->childrenRows; } else { d->topLevelColumns += count; if (d->horizontalHeader.size() > column) d->horizontalHeader.insert(column, count, 0); } // update any item vectors if needed for (int i=0; i<rows->count(); ++i) { QStdModelRow *modelRow = rows->at(i); // only insert if there is a QStdModelRow and QStdModelItems in it if (modelRow && modelRow->items.count() && modelRow->items.count() > column) modelRow->items.insert(column, count, 0); } endInsertColumns(); return true;}/*! Removes \a count rows from the model, starting with the specified \a row. The items removed are children of the item represented by the given \a parent model index. Returns true if the rows were successfully removed; otherwise returns false. \sa clear(), insertRows(), rowCount()*/bool QStandardItemModel::removeRows(int row, int count, const QModelIndex &parent){ Q_D(QStandardItemModel); if (count < 1 || row < 0 || (row + count) > rowCount(parent)) return false; beginRemoveRows(parent, row, row + count - 1); QVector<QStdModelRow*> &rows = (parent.isValid()) ? d->containedRow(parent, true)->childrenRows : d->topLevelRows; if (!parent.isValid() && d->verticalHeader.count() > row) { int headerCount = qMin(d->verticalHeader.count() - row, count); for (int i=row; i < (row+headerCount); ++i) delete d->verticalHeader.at(i); d->verticalHeader.remove(row, headerCount); } // delete QStdModelRows for (int i=row; i<(row+count); ++i) delete rows.at(i); // resize row vector rows.remove(row, count); endRemoveRows(); return true;}/*! Removes \a count columns from the model, starting with the specified \a column. The items removed are children of the item represented by the given \a parent model index. Returns true if the columns were successfully removed; otherwise returns false. \sa clear(), insertColumns(), columnCount()*/bool QStandardItemModel::removeColumns(int column, int count, const QModelIndex &parent){ Q_D(QStandardItemModel); // The implimentation of this class means you can't have columns without rows. if (count < 1 || column < 0 || (column + count) > columnCount(parent) || rowCount(parent) == 0) return false; beginRemoveColumns(parent, column, column + count - 1); QVector<QStdModelRow*> *rows = &d->topLevelRows; // update the column counters if (parent.isValid()) { QStdModelRow *modelRow = d->containedRow(parent, true); modelRow->childrenColumns -= count; rows = &modelRow->childrenRows; } else { d->topLevelColumns -= count; if (d->horizontalHeader.count() > column) { int headerCount = qMin(d->horizontalHeader.size() - column, count); for (int i=column; i < (column+headerCount); ++i) delete d->horizontalHeader.at(i); d->horizontalHeader.remove(column, headerCount); } } // iterate over all child rows and remove if needed for (int i=0; i<rows->count(); ++i) { QStdModelRow *modelRow = rows->at(i); if (modelRow && column < modelRow->items.count()) { // delete the QStdModelItem if any for (int j=column; j<(column+count) && j<modelRow->items.count(); ++j) delete modelRow->items.at(j); // resize item vector modelRow->items.remove(column, qMin(count, modelRow->items.count() - column)); } } endRemoveColumns(); return true;}/*! Returns the item flags for the given \a index. This model returns returns a combination of flags that enables the item (Qt::ItemIsEnabled), allows it to be selected (Qt::ItemIsSelectable) and edited (Qt::ItemIsEditable). \sa Qt::ItemFlags*/Qt::ItemFlags QStandardItemModel::flags(const QModelIndex &index) const{ if (!index.isValid()) return Qt::ItemIsDropEnabled; return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;}/*! Removes all items from the model and resets both the horizontal and vertical headers. \sa removeColumns(), removeRows()*/void QStandardItemModel::clear(){ Q_D(QStandardItemModel); d->clear(); reset();}/*! \internal*/QStandardItemModelPrivate::~QStandardItemModelPrivate(){ clear();}/*! \internal*/QStdModelRow *QStandardItemModelPrivate::containedRow(const QModelIndex &index, bool createIfMissing) const{ if (!index.isValid()) return 0; QStdModelRow *parentRow = static_cast<QStdModelRow*>(index.internalPointer()); QVector<QStdModelRow*> *rowList = !parentRow ? (&topLevelRows) : (&parentRow->childrenRows); if (createIfMissing && !rowList->at(index.row())) rowList->replace(index.row() , new QStdModelRow(parentRow)); return rowList->at(index.row());}/*! \internal*/void QStandardItemModelPrivate::clear(){ topLevelColumns = 0; qDeleteAll(topLevelRows); topLevelRows.clear(); qDeleteAll(horizontalHeader); horizontalHeader.clear(); qDeleteAll(verticalHeader); verticalHeader.clear();}#endif // QT_NO_STANDARDITEMMODEL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -