📄 qstandarditemmodel.cpp
字号:
*/void QStandardItem::insertColumn(int column, const QList<QStandardItem*> &items){ Q_D(QStandardItem); if (column < 0) return; if (rowCount() < items.count()) setRowCount(items.count()); d->insertColumns(column, 1, items);}/*! Inserts \a count rows of child items at row \a row. \sa insertRow(), insertColumns()*/void QStandardItem::insertRows(int row, int count){ Q_D(QStandardItem); if (rowCount() < row) { count += row - rowCount(); row = rowCount(); } d->insertRows(row, count, QList<QStandardItem*>());}/*! Inserts \a count columns of child items at column \a column. \sa insertColumn(), insertRows()*/void QStandardItem::insertColumns(int column, int count){ Q_D(QStandardItem); if (columnCount() < column) { count += column - columnCount(); column = columnCount(); } d->insertColumns(column, count, QList<QStandardItem*>());}/*! \fn void QStandardItem::appendRow(const QList<QStandardItem*> &items) Appends a row containing \a items. If necessary, the column count is increased to the size of \a items. \sa insertRow()*//*! \fn void QStandardItem::appendRows(const QList<QStandardItem*> &items) Appends rows containing \a items. The column count will not change. \sa insertRow()*//*! \fn void QStandardItem::appendColumn(const QList<QStandardItem*> &items) Appends a column containing \a items. If necessary, the row count is increased to the size of \a items. \sa insertColumn()*//*! \fn bool QStandardItemModel::insertRow(int row, const QModelIndex &parent) Inserts a single row before the given \a row in the child items of the \a parent specified. Returns true if the row is inserted; otherwise returns false. \sa insertRows(), insertColumn(), removeRow()*//*! \fn bool QStandardItemModel::insertColumn(int column, const QModelIndex &parent) Inserts a single column before the given \a column in the child items of the \a parent specified. Returns true if the column is inserted; otherwise returns false. \sa insertColumns(), insertRow(), removeColumn()*//*! \fn QStandardItem::insertRow(int row, QStandardItem *item) \overload Inserts a row at \a row containing \a item. When building a list or a tree that has only one column, this function provides a convenient way to insert a single new item.*//*! \fn QStandardItem::appendRow(QStandardItem *item) \overload Appends a row containing \a item. When building a list or a tree that has only one column, this function provides a convenient way to append a single new item.*//*! Removes the given \a row. The items that were in the row are deleted. \sa takeRow(), removeRows(), removeColumn()*/void QStandardItem::removeRow(int row){ removeRows(row, 1);}/*! Removes the given \a column. The items that were in the column are deleted. \sa takeColumn(), removeColumns(), removeRow()*/void QStandardItem::removeColumn(int column){ removeColumns(column, 1);}/*! Removes \a count rows at row \a row. The items that were in those rows are deleted. \sa removeRow(), removeColumn()*/void QStandardItem::removeRows(int row, int count){ Q_D(QStandardItem); if ((count < 1) || (row < 0) || ((row + count) > rowCount())) return; if (d->model) d->model->d_func()->rowsAboutToBeRemoved(this, row, row + count - 1); int i = d->childIndex(row, 0); int n = count * d->columnCount(); for (int j = i; j < n+i; ++j) { QStandardItem *oldItem = d->children.at(j); if (oldItem) oldItem->d_func()->setModel(0); delete oldItem; } d->children.remove(qMax(i, 0), n); d->rows -= count; if (d->model) d->model->d_func()->rowsRemoved(this, row, count);}/*! Removes \a count columns at column \a column. The items that were in those columns are deleted. \sa removeColumn(), removeRows()*/void QStandardItem::removeColumns(int column, int count){ Q_D(QStandardItem); if ((count < 1) || (column < 0) || ((column + count) > columnCount())) return; if (d->model) d->model->d_func()->columnsAboutToBeRemoved(this, column, column + count - 1); for (int row = d->rowCount() - 1; row >= 0; --row) { int i = d->childIndex(row, column); for (int j=i; j<i+count; ++j) { QStandardItem *oldItem = d->children.at(j); if (oldItem) oldItem->d_func()->setModel(0); delete oldItem; } d->children.remove(i, count); } d->columns -= count; if (d->model) d->model->d_func()->columnsRemoved(this, column, count);}/*! Returns true if this item has any children; otherwise returns false. \sa rowCount(), columnCount(), child()*/bool QStandardItem::hasChildren() const{ return (rowCount() > 0) && (columnCount() > 0);}/*! Sets the child item at (\a row, \a column) to \a item. This item (the parent item) takes ownership of \a item. If necessary, the row count and column count are increased to fit the item. \sa child()*/void QStandardItem::setChild(int row, int column, QStandardItem *item){ Q_D(QStandardItem); d->setChild(row, column, item, true);}/*! \fn QStandardItem::setChild(int row, QStandardItem *item) \overload Sets the child at \a row to \a item.*//*! Returns the child item at (\a row, \a column) if one has been set; otherwise returns 0. \sa setChild(), takeChild(), parent()*/QStandardItem *QStandardItem::child(int row, int column) const{ Q_D(const QStandardItem); int index = d->childIndex(row, column); if (index == -1) return 0; return d->children.at(index);}/*! Removes the child item at (\a row, \a column) without deleting it, and returns a pointer to the item. If there was no child at the given location, then this function returns 0. Note that this function, unlike takeRow() and takeColumn(), does not affect the dimensions of the child table. \sa child(), takeRow(), takeColumn()*/QStandardItem *QStandardItem::takeChild(int row, int column){ Q_D(QStandardItem); QStandardItem *item = 0; int index = d->childIndex(row, column); if (index != -1) { item = d->children.at(index); d->children.replace(index, 0); if (item) item->d_func()->setParentAndModel(0, 0); } return item;}/*! Removes \a row without deleting the row items, and returns a list of pointers to the removed items. For items in the row that have not been set, the corresponding pointers in the list will be 0. \sa removeRow(), insertRow(), takeColumn()*/QList<QStandardItem*> QStandardItem::takeRow(int row){ Q_D(QStandardItem); if ((row < 0) || (row >= rowCount())) return QList<QStandardItem*>(); QList<QStandardItem*> items; int index = d->childIndex(row, 0); for (int column = 0; column < d->columnCount(); ++column) { QStandardItem *ch = d->children.at(index); if (ch) { ch->d_func()->setParentAndModel(0, 0); d->children.replace(index, 0); } items.append(ch); ++index; } removeRow(row); return items;}/*! Removes \a column without deleting the column items, and returns a list of pointers to the removed items. For items in the column that have not been set, the corresponding pointers in the list will be 0. \sa removeColumn(), insertColumn(), takeRow()*/QList<QStandardItem*> QStandardItem::takeColumn(int column){ Q_D(QStandardItem); if ((column < 0) || (column >= columnCount())) return QList<QStandardItem*>(); QList<QStandardItem*> items; int index = d->childIndex(0, column); for (int row = 0; row < d->rowCount(); ++row) { QStandardItem *ch = d->children.at(index); if (ch) { ch->d_func()->setParentAndModel(0, 0); d->children.replace(index, 0); } items.append(ch); index += d->columnCount(); } removeColumn(column); return items;}/*! Returns true if this item is less than \a other; otherwise returns false. The default implementation uses the data for the item's sort role (see QStandardItemModel::sortRole) to perform the comparison if the item belongs to a model; otherwise, the data for the item's Qt::DisplayRole (text()) is used to perform the comparison. sortChildren() and QStandardItemModel::sort() use this function when sorting items. If you want custom sorting, you can subclass QStandardItem and reimplement this function.*/bool QStandardItem::operator<(const QStandardItem &other) const{ const int role = model() ? model()->sortRole() : Qt::DisplayRole; const QVariant l = data(role), r = other.data(role); // this code is copied from QSortFilterProxyModel::lessThan() switch (l.type()) { case QVariant::Invalid: return (r.type() == QVariant::Invalid); case QVariant::Int: return l.toInt() < r.toInt(); case QVariant::UInt: return l.toUInt() < r.toUInt(); case QVariant::LongLong: return l.toLongLong() < r.toLongLong(); case QVariant::ULongLong: return l.toULongLong() < r.toULongLong(); case QVariant::Double: return l.toDouble() < r.toDouble(); case QVariant::Char: return l.toChar() < r.toChar(); case QVariant::Date: return l.toDate() < r.toDate(); case QVariant::Time: return l.toTime() < r.toTime(); case QVariant::DateTime: return l.toDateTime() < r.toDateTime(); case QVariant::String: default: return l.toString().compare(r.toString()) < 0; }}/*! Sorts the children of the item using the given \a order, by the values in the given \a column. \sa {operator<()}*/void QStandardItem::sortChildren(int column, Qt::SortOrder order){ Q_D(QStandardItem); if ((column < 0) || (rowCount() == 0)) return; if (d->model) emit d->model->layoutAboutToBeChanged(); d->sortChildren(column, order); if (d->model) emit d->model->layoutChanged();}/*! Returns a copy of this item. The item's children are not copied. When subclassing QStandardItem, you can reimplement this function to provide QStandardItemModel with a factory that it can use to create new items on demand. \sa QStandardItemModel::setItemPrototype(), operator=()*/QStandardItem *QStandardItem::clone() const{ return new QStandardItem(*this);}/*! Returns the type of this item. The type is used to distinguish custom items from the base class. When subclassing QStandardItem, you should reimplement this function and return a new value greater than or equal to \l UserType. \sa QStandardItem::Type*/int QStandardItem::type() const{ return Type;}#ifndef QT_NO_DATASTREAM/*! Reads the item from stream \a in. Only the data and flags of the item are read, not the child items. \sa write()*/void QStandardItem::read(QDataStream &in){ Q_D(QStandardItem); in >> d->values; qint32 flags; in >> flags; setFlags((Qt::ItemFlags)flags);}/*! Writes the item to stream \a out. Only the data and flags of the item are written, not the child items. \sa read()*/void QStandardItem::write(QDataStream &out) const{ Q_D(const QStandardItem); out << d->values; out << flags();}/*! \relates QStandardItem \since 4.2 Reads a QStandardItem from stream \a in into \a item. This operator uses QStandardItem::read(). \sa {Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &in, QStandardItem &item){ item.read(in); return in;}/*! \relates QStandardItem \since 4.2 Writes the QStandardItem \a item to stream \a out. This operator uses QStandardItem::write(). \sa {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &out, const QStandardItem &item){ item.write(out); return out;}#endif // !QT_NO_DATASTREAM/*! \class QStandardItemModel \brief The QStandardItemModel class provides a generic model for storing custom data. \ingroup model-view QStandardItemModel can be used as a repository for standard Qt data types. It is one of the \l {Model/View Classes} and is part of Qt's \l {Model/View Programming}{model/view} framework. QStandardItemModel provides a classic item-based approach to working with the model. The items in a QStandardItemModel are provided by QStandardItem.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -