📄 qstandarditemmodel.cpp
字号:
QStandardItemModel implements the QAbstractItemModel interface, which means that the model can be used to provide data in any view that supports that interface (such as QListView, QTableView and QTreeView, and your own custom views). For performance and flexibility, you may want to subclass QAbstractItemModel to provide support for different kinds of data repositories. For example, the QDirModel provides a model interface to the underlying file system, and does not actually store file information internally. When you want a list or tree, you typically create an empty QStandardItemModel and use appendRow() to add items to the model, and item() to access an item. If your model represents a table, you typically pass the dimensions of the table to the QStandardItemModel constructor and use setItem() to position items into the table. You can also use setRowCount() and setColumnCount() to alter the dimensions of the model. To insert items, use insertRow() or insertColumn(), and to remove items, use removeRow() or removeColumn(). You can set the header labels of your model with setHorizontalHeaderLabels() and setVerticalHeaderLabels(). You can search for items in the model with findItems(), and sort the model by calling sort(). Call clear() to remove all items from the model. An example usage of QStandardItemModel to create a table: \code QStandardItemModel model(4, 4); for (int row = 0; row < 4; ++row) { for (int column = 0; column < 4; ++column) { QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column)); model.setItem(row, column, item); } } \endcode An example usage of QStandardItemModel to create a tree: \code QStandardItemModel model; QStandardItem *parentItem = model.invisibleRootItem(); for (int i = 0; i < 4; ++i) { QStandardItem *item = new QStandardItem(QString("item %0").arg(i)); parentItem->appendRow(item); parentItem = item; } \endcode After setting the model on a view, you typically want to react to user actions, such as an item being clicked. Since a QAbstractItemView provides QModelIndex-based signals and functions, you need a way to obtain the QStandardItem that corresponds to a given QModelIndex, and vice versa. itemFromIndex() and indexFromItem() provide this mapping. Typical usage of itemFromIndex() includes obtaining the item at the current index in a view, and obtaining the item that corresponds to an index carried by a QAbstractItemView signal, such as QAbstractItemView::clicked(). First you connect the view's signal to a slot in your class: \code QTreeView *treeView = new QTreeView(this); treeView->setModel(myStandardItemModel); connect(treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(clicked(QModelIndex))); \endcode When you receive the signal, you call itemFromIndex() on the given model index to get a pointer to the item: \code void MyWidget::clicked(const QModelIndex &index) { QStandardItem *item = myStandardItemModel->itemFromIndex(index); // Do stuff with the item ... } \endcode Conversely, you must obtain the QModelIndex of an item when you want to invoke a model/view function that takes an index as argument. You can obtain the index either by using the model's indexFromItem() function, or, equivalently, by calling QStandardItem::index(): \code treeView->scrollTo(item->index()); \endcode You are, of course, not required to use the item-based approach; you could instead rely entirely on the QAbstractItemModel interface when working with the model, or use a combination of the two as appropriate. \sa QStandardItem, {Model/View Programming}, QAbstractItemModel, {itemviews/simpletreemodel}{Simple Tree Model example}, {Item View Convenience Classes}*//*! \fn void QStandardItemModel::itemChanged(QStandardItem *item) \since 4.2 This signal is emitted whenever the data of \a item has changed.*//*! Constructs a new item model with the given \a parent.*/QStandardItemModel::QStandardItemModel(QObject *parent) : QAbstractItemModel(*new QStandardItemModelPrivate, parent){ Q_D(QStandardItemModel); d->init(); d->root->d_func()->setModel(this);}/*! Constructs a new item model that initially has \a rows rows and \a columns columns, and that has the given \a parent.*/QStandardItemModel::QStandardItemModel(int rows, int columns, QObject *parent) : QAbstractItemModel(*new QStandardItemModelPrivate, parent){ Q_D(QStandardItemModel); d->init(); d->root->insertColumns(0, columns); d->columnHeaderItems.insert(0, columns, 0); d->root->insertRows(0, rows); d->rowHeaderItems.insert(0, rows, 0); d->root->d_func()->setModel(this);}/*! \internal*/QStandardItemModel::QStandardItemModel(QStandardItemModelPrivate &dd, QObject *parent) : QAbstractItemModel(dd, parent){ Q_D(QStandardItemModel); d->init();}/*! Destructs the model. The model destroys all its items.*/QStandardItemModel::~QStandardItemModel(){}/*! Removes all items (including header items) from the model and sets the number of rows and columns to zero. \sa removeColumns(), removeRows()*/void QStandardItemModel::clear(){ Q_D(QStandardItemModel); delete d->root; d->root = new QStandardItem; d->root->d_func()->setModel(this); qDeleteAll(d->columnHeaderItems); d->columnHeaderItems.clear(); qDeleteAll(d->rowHeaderItems); d->rowHeaderItems.clear(); reset();}/*! \since 4.2 Returns a pointer to the QStandardItem associated with the given \a index. Calling this function is typically the initial step when processing QModelIndex-based signals from a view, such as QAbstractItemView::activated(). In your slot, you call itemFromIndex(), with the QModelIndex carried by the signal as argument, to obtain a pointer to the corresponding QStandardItem. Note that this function will lazily create an item for the index (using itemPrototype()), and set it in the parent item's child table, if no item already exists at that index. If \a index is an invalid index, this function returns 0. \sa indexFromItem()*/QStandardItem *QStandardItemModel::itemFromIndex(const QModelIndex &index) const{ Q_D(const QStandardItemModel); if ((index.row() < 0) || (index.column() < 0) || (index.model() != this)) return 0; QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer()); if (parent == 0) return 0; QStandardItem *item = parent->child(index.row(), index.column()); // lazy part if (item == 0) { item = d->createItem(); parent->d_func()->setChild(index.row(), index.column(), item); } return item;}/*! \since 4.2 Returns the QModelIndex associated with the given \a item. Use this function when you want to perform an operation that requires the QModelIndex of the item, such as QAbstractItemView::scrollTo(). QStandardItem::index() is provided as convenience; it is equivalent to calling this function. \sa itemFromIndex(), QStandardItem::index()*/QModelIndex QStandardItemModel::indexFromItem(const QStandardItem *item) const{ if (item && item->d_func()->parent) { QPair<int, int> pos = item->d_func()->position(); return createIndex(pos.first, pos.second, item->d_func()->parent); } return QModelIndex();}/*! \since 4.2 Sets the number of rows in this model to \a rows. If this is less than rowCount(), the data in the unwanted rows is discarded. \sa setColumnCount()*/void QStandardItemModel::setRowCount(int rows){ Q_D(QStandardItemModel); d->root->setRowCount(rows);}/*! \since 4.2 Sets the number of columns in this model to \a columns. If this is less than columnCount(), the data in the unwanted columns is discarded. \sa setRowCount()*/void QStandardItemModel::setColumnCount(int columns){ Q_D(QStandardItemModel); d->root->setColumnCount(columns);}/*! \since 4.2 Sets the item for the given \a row and \a column to \a item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item. The previous item at the given location (if there was one) is deleted. \sa item()*/void QStandardItemModel::setItem(int row, int column, QStandardItem *item){ Q_D(QStandardItemModel); d->root->d_func()->setChild(row, column, item, true);}/*! \fn QStandardItemModel::setItem(int row, QStandardItem *item) \overload*//*! \since 4.2 Returns the item for the given \a row and \a column if one has been set; otherwise returns 0. \sa setItem(), takeItem(), itemFromIndex()*/QStandardItem *QStandardItemModel::item(int row, int column) const{ Q_D(const QStandardItemModel); return d->root->child(row, column);}/*! \since 4.2 Returns the model's invisible root item. The invisible root item provides access to the model's top-level items through the QStandardItem API, making it possible to write functions that can treat top-level items and their children in a uniform way; for example, recursive functions involving a tree model.*/QStandardItem *QStandardItemModel::invisibleRootItem() const{ Q_D(const QStandardItemModel); return d->root;}/*! \since 4.2 Sets the horizontal header item for \a column to \a item. The model takes ownership of the item. If necessary, the column count is increased to fit the item. The previous header item (if there was one) is deleted. \sa horizontalHeaderItem(), setHorizontalHeaderLabels(), setVerticalHeaderItem()*/void QStandardItemModel::setHorizontalHeaderItem(int column, QStandardItem *item){ Q_D(QStandardItemModel); if (column < 0) return; if (columnCount() <= column) setColumnCount(column + 1); QStandardItem *oldItem = d->columnHeaderItems.at(column); if (item == oldItem) return; if (item) { if (item->model() == 0) { item->d_func()->setModel(this); } else { qWarning("QStandardItem::setHorizontalHeaderItem: Ignoring duplicate insertion of item %p", item); return; } } if (oldItem) oldItem->d_func()->setModel(0); delete oldItem; d->columnHeaderItems.replace(column, item); emit headerDataChanged(Qt::Horizontal, column, column);}/*! \since 4.2 Returns the horizontal header item for \a column if one has been set; otherwise returns 0. \sa setHorizontalHeaderItem(), verticalHeaderItem()*/QStandardItem *QStandardItemModel::horizontalHeaderItem(int column) const{ Q_D(const QStandardItemModel); if ((column < 0) || (column >= columnCount())) return 0; return d->columnHeaderItems.at(column);}/*! \since 4.2 Sets the vertical header item for \a row to \a item. The model takes ownership of the item. If necessary, the row count is increased to fit the item. The previous header item (if there was one) is deleted. \sa verticalHeaderItem(), setVerticalHeaderLabels(), setHorizontalHeaderItem()*/void QStandardItemModel::setVerticalHeaderItem(int row, QStandardItem *item){ Q_D(QStandardItemModel); if (row < 0) return; if (rowCount() <= row) setRowCount(row + 1); QStandardItem *oldItem = d->rowHeaderItems.at(row); if (item == oldItem) return; if (item) { if (item->model() == 0) { item->d_func()->setModel(this); } else { qWarning("QStandardItem::setVerticalHeaderItem: Ignoring duplicate insertion of item %p", item); return; } } if (oldItem) oldItem->d_func()->setModel(0); delete oldItem; d->rowHeaderItems.replace(row, item); emit headerDataChanged(Qt::Vertical, row, row);}/*! \since 4.2 Returns the vertical header item for row \a row if one has been set; otherwise returns 0. \sa setVerticalHeaderItem(), horizontalHeaderItem()*/QStandardItem *QStandardItemModel::verticalHeaderItem(int row) const{ Q_D(const QStandardItemModel); if ((row < 0) || (row >= rowCount())) return 0; return d->rowHeaderItems.at(row);}/*! \since 4.2 Sets the horizontal header labels using \a labels. If necessary, the column count is increased to the size of \a labels. \sa setHorizontalHeaderItem()*/void QStandardItemModel::setHorizontalHeaderLabels(const QStringList &labels){ Q_D(QStandardItemModel); if (columnCount() < labels.count()) setColumnCount(labels.count()); for (int i = 0; i < labels.count(); ++i) { QStandardItem *item = horizontalHeaderItem(i); if (!item) { item = d->createItem(); setHorizontalHeaderItem(i, item); } item->setText(labels.at(i)); }}/*! \since 4.2 Sets the vertical header labels using \a labels. If necessary, the row count is increased to the size of \a labels. \sa setVerticalHeaderItem()*/void QStandardItemModel::setVerticalHeaderLabels(const QStringList &labels){ Q_D(QStandardItemModel); if (rowCount() < labels.count()) setRowCount(labels.count()); for (int i = 0; i < labels.count(); ++i) { QStandardItem *item = verticalHeaderItem(i); if (!item) { item = d->createItem(); setVerticalHeaderItem(i, item); } item->setText(labels.at(i)); }}/*! \since 4.2 Sets the item prototype for the model to the specified \a item. The model takes ownership of the prototype. The item prototype acts as a QStandardItem factory, by relying on the QStandardItem::clone() functi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -