The QStandardItemModel class provides a generic model for storing custom data. 更多...
| 頭: | #include <QStandardItemModel> |
| qmake: | QT += gui |
| 繼承: | QAbstractItemModel |
| QStandardItemModel (QObject * parent = nullptr) | |
| QStandardItemModel (int rows , int columns , QObject * parent = nullptr) | |
| virtual | ~QStandardItemModel () |
| void | appendColumn (const QList<QStandardItem *> & items ) |
| void | appendRow (const QList<QStandardItem *> & items ) |
| void | appendRow (QStandardItem * item ) |
| void | clear () |
| bool | clearItemData (const QModelIndex & index ) |
| QList<QStandardItem *> | findItems (const QString & text , Qt::MatchFlags flags = Qt::MatchExactly, int column = 0) const |
| QStandardItem * | horizontalHeaderItem (int column ) const |
| QModelIndex | indexFromItem (const QStandardItem * item ) const |
| void | insertColumn (int column , const QList<QStandardItem *> & items ) |
| bool | insertColumn (int column , const QModelIndex & parent = QModelIndex()) |
| void | insertRow (int row , const QList<QStandardItem *> & items ) |
| void | insertRow (int row , QStandardItem * item ) |
| bool | insertRow (int row , const QModelIndex & parent = QModelIndex()) |
| QStandardItem * | invisibleRootItem () const |
| QStandardItem * | item (int row , int column = 0) const |
| QStandardItem * | itemFromIndex (const QModelIndex & index ) const |
| const QStandardItem * | itemPrototype () const |
| void | setColumnCount (int columns ) |
| void | setHorizontalHeaderItem (int column , QStandardItem * item ) |
| void | setHorizontalHeaderLabels (const QStringList & labels ) |
| void | setItem (int row , int column , QStandardItem * item ) |
| void | setItem (int row , QStandardItem * item ) |
| void | setItemPrototype (const QStandardItem * item ) |
| void | setItemRoleNames (const QHash<int, QByteArray> & roleNames ) |
| void | setRowCount (int rows ) |
| void | setSortRole (int role ) |
| void | setVerticalHeaderItem (int row , QStandardItem * item ) |
| void | setVerticalHeaderLabels (const QStringList & labels ) |
| int | sortRole () const |
| QList<QStandardItem *> | takeColumn (int column ) |
| QStandardItem * | takeHorizontalHeaderItem (int column ) |
| QStandardItem * | takeItem (int row , int column = 0) |
| QList<QStandardItem *> | takeRow (int row ) |
| QStandardItem * | takeVerticalHeaderItem (int row ) |
| QStandardItem * | verticalHeaderItem (int row ) const |
| virtual int | columnCount (const QModelIndex & parent = QModelIndex()) const override |
| virtual QVariant | data (const QModelIndex & index , int role = Qt::DisplayRole) const override |
| virtual bool | dropMimeData (const QMimeData * data , Qt::DropAction action , int row , int column , const QModelIndex & parent ) override |
| virtual Qt::ItemFlags | flags (const QModelIndex & index ) const override |
| virtual bool | hasChildren (const QModelIndex & parent = QModelIndex()) const override |
| virtual QVariant | headerData (int section , Qt::Orientation orientation , int role = Qt::DisplayRole) const override |
| virtual QModelIndex | index (int row , int column , const QModelIndex & parent = QModelIndex()) const override |
| virtual bool | insertColumns (int column , int count , const QModelIndex & parent = QModelIndex()) override |
| virtual bool | insertRows (int row , int count , const QModelIndex & parent = QModelIndex()) override |
| virtual QMap<int, QVariant> | itemData (const QModelIndex & index ) const override |
| virtual QMimeData * | mimeData (const QModelIndexList & indexes ) const override |
| virtual QStringList | mimeTypes () const override |
| virtual QModelIndex | parent (const QModelIndex & child ) const override |
| virtual bool | removeColumns (int column , int count , const QModelIndex & parent = QModelIndex()) override |
| virtual bool | removeRows (int row , int count , const QModelIndex & parent = QModelIndex()) override |
| virtual int | rowCount (const QModelIndex & parent = QModelIndex()) const override |
| virtual bool | setData (const QModelIndex & index , const QVariant & value , int role = Qt::EditRole) override |
| virtual bool | setHeaderData (int section , Qt::Orientation orientation , const QVariant & value , int role = Qt::EditRole) override |
| virtual bool | setItemData (const QModelIndex & index , const QMap<int, QVariant> & roles ) override |
| virtual QModelIndex | sibling (int row , int column , const QModelIndex & idx ) const override |
| virtual void | sort (int column , Qt::SortOrder order = Qt::AscendingOrder) override |
| virtual Qt::DropActions | supportedDropActions () const override |
| void | itemChanged (QStandardItem * item ) |
| const QMetaObject | staticMetaObject |
The QStandardItemModel class provides a generic model for storing custom data.
QStandardItemModel can be used as a repository for standard Qt data types. It is one of the 模型/視圖類 且屬於 Qt 的 模型/視圖 框架。
QStandardItemModel provides a classic item-based approach to working with the model. The items in a QStandardItemModel 的提供是通過 QStandardItem .
QStandardItemModel 實現 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.
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 () 和 setColumnCount () to alter the dimensions of the model. To insert items, use insertRow () 或 insertColumn (), and to remove items, use removeRow () 或 removeColumn ().
You can set the header labels of your model with setHorizontalHeaderLabels () 和 setVerticalHeaderLabels ().
You can search for items in the model with findItems (), and sort the model by calling sort ().
調用 clear () to remove all items from the model.
An example usage of QStandardItemModel to create a table:
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); } }
An example usage of QStandardItemModel to create a tree:
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; }
After setting the model on a view, you typically want to react to user actions, such as an item being clicked. Since a QAbstractItemView 提供 QModelIndex -based signals and functions, you need a way to obtain the QStandardItem that corresponds to a given QModelIndex , and vice versa. itemFromIndex () 和 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:
QTreeView *treeView = new QTreeView(this); treeView->setModel(myStandardItemModel); connect(treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(clicked(QModelIndex)));
When you receive the signal, you call itemFromIndex () on the given model index to get a pointer to the item:
void MyWidget::clicked(const QModelIndex &index) { QStandardItem *item = myStandardItemModel->itemFromIndex(index); // Do stuff with the item ... }
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 ():
treeView->scrollTo(item->index());
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.
另請參閱 QStandardItem , 模型/視圖編程 , QAbstractItemModel , Simple Tree Model example ,和 項視圖方便類 .
This property holds the item role that is used to query the model's data when sorting items
默認值為 Qt::DisplayRole .
該特性在 Qt 4.2 引入。
訪問函數:
| int | sortRole () const |
| void | setSortRole (int role ) |
另請參閱 sort () 和 QStandardItem::sortChildren ().
Constructs a new item model with the given parent .
Constructs a new item model that initially has rows rows and columns columns, and that has the given parent .
[虛擬]
QStandardItemModel::
~QStandardItemModel
()
Destructs the model. The model destroys all its items.
Appends a column containing items . If necessary, the row count is increased to the size of items .
該函數在 Qt 4.2 引入。
另請參閱 insertColumn () 和 appendRow ().
Appends a row containing items . If necessary, the column count is increased to the size of items .
該函數在 Qt 4.2 引入。
另請參閱 insertRow () 和 appendColumn ().
這是重載函數。
When building a list or a tree that has only one column, this function provides a convenient way to append a single new item .
該函數在 Qt 4.2 引入。
Removes all items (including header items) from the model and sets the number of rows and columns to zero.
另請參閱 removeColumns () 和 removeRows ().
移除在所有角色中存儲的數據,為給定
index
。返迴
true
if
index
is valid and data was cleared,
false
否則。
該函數在 Qt 5.12 引入。
[override virtual]
int
QStandardItemModel::
columnCount
(const
QModelIndex
&
parent
= QModelIndex()) const
重實現自 QAbstractItemModel::columnCount ().
另請參閱 setColumnCount ().
[override virtual]
QVariant
QStandardItemModel::
data
(const
QModelIndex
&
index
,
int
role
= Qt::DisplayRole) const
重實現自 QAbstractItemModel::data ().
另請參閱 setData ().
[override virtual]
bool
QStandardItemModel::
dropMimeData
(const
QMimeData
*
data
,
Qt::DropAction
action
,
int
row
,
int
column
, const
QModelIndex
&
parent
)
重實現自 QAbstractItemModel::dropMimeData ().
返迴的項列錶匹配給定 text ,使用給定 flags ,在給定 column .
該函數在 Qt 4.2 引入。
[override virtual]
Qt::ItemFlags
QStandardItemModel::
flags
(const
QModelIndex
&
index
) const
重實現自 QAbstractItemModel::flags ().
[override virtual]
bool
QStandardItemModel::
hasChildren
(const
QModelIndex
&
parent
= QModelIndex()) const
重實現自 QAbstractItemModel::hasChildren ().
[override virtual]
QVariant
QStandardItemModel::
headerData
(
int
section
,
Qt::Orientation
orientation
,
int
role
= Qt::DisplayRole) const
重實現自 QAbstractItemModel::headerData ().
另請參閱 setHeaderData ().
Returns the horizontal header item for
column
若有設置;否則返迴
nullptr
.
該函數在 Qt 4.2 引入。
另請參閱 setHorizontalHeaderItem () 和 verticalHeaderItem ().
[override virtual]
QModelIndex
QStandardItemModel::
index
(
int
row
,
int
column
, const
QModelIndex
&
parent
= QModelIndex()) const
重實現自 QAbstractItemModel::index ().
返迴 QModelIndex 關聯給定 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.
該函數在 Qt 4.2 引入。
另請參閱 itemFromIndex () 和 QStandardItem::index ().
Inserts a column at column 包含 items . If necessary, the row count is increased to the size of items .
該函數在 Qt 4.2 引入。
另請參閱 takeColumn (), appendColumn (),和 insertRow ().
插入單列先於給定
column
in the child items of the
parent
指定。返迴
true
若列被插入;否則返迴
false
.
另請參閱 insertColumns (), insertRow (),和 removeColumn ().
[override virtual]
bool
QStandardItemModel::
insertColumns
(
int
column
,
int
count
, const
QModelIndex
&
parent
= QModelIndex())
重實現自 QAbstractItemModel::insertColumns ().
Inserts a row at row 包含 items . If necessary, the column count is increased to the size of items .
該函數在 Qt 4.2 引入。
另請參閱 takeRow (), appendRow (),和 insertColumn ().
這是重載函數。
Inserts a row at row 包含 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.
該函數在 Qt 4.2 引入。
插入單行先於給定
row
in the child items of the
parent
指定。返迴
true
若行被插入;否則返迴
false
.
另請參閱 insertRows (), insertColumn (),和 removeRow ().
[override virtual]
bool
QStandardItemModel::
insertRows
(
int
row
,
int
count
, const
QModelIndex
&
parent
= QModelIndex())
重實現自 QAbstractItemModel::insertRows ().
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.
注意: 調用 index() 在 QStandardItem object retrieved from this function is not valid.
該函數在 Qt 4.2 引入。
返迴項為給定
row
and
column
若有設置;否則返迴
nullptr
.
該函數在 Qt 4.2 引入。
另請參閱 setItem (), takeItem (),和 itemFromIndex ().
[signal]
void
QStandardItemModel::
itemChanged
(
QStandardItem
*
item
)
此信號發射,每當數據對於 item 已改變。
該函數在 Qt 4.2 引入。
[override virtual]
QMap
<
int
,
QVariant
> QStandardItemModel::
itemData
(const
QModelIndex
&
index
) const
重實現自 QAbstractItemModel::itemData ().
另請參閱 setItemData ().
返迴指針指嚮 QStandardItem 關聯給定 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.
若
index
is an invalid index, this function returns
nullptr
.
該函數在 Qt 4.2 引入。
另請參閱 indexFromItem ().
Returns the item prototype used by the model. The model uses the item prototype as an item factory when it needs to construct new items on demand (for instance, when a view or item delegate calls setData ()).
該函數在 Qt 4.2 引入。
另請參閱 setItemPrototype ().
[override virtual]
QMimeData
*QStandardItemModel::
mimeData
(const
QModelIndexList
&
indexes
) const
重實現自 QAbstractItemModel::mimeData ().
[override virtual]
QStringList
QStandardItemModel::
mimeTypes
() const
重實現自 QAbstractItemModel::mimeTypes ().
[override virtual]
QModelIndex
QStandardItemModel::
parent
(const
QModelIndex
&
child
) const
重實現自 QAbstractItemModel::parent ().
[override virtual]
bool
QStandardItemModel::
removeColumns
(
int
column
,
int
count
, const
QModelIndex
&
parent
= QModelIndex())
重實現自 QAbstractItemModel::removeColumns ().
[override virtual]
bool
QStandardItemModel::
removeRows
(
int
row
,
int
count
, const
QModelIndex
&
parent
= QModelIndex())
重實現自 QAbstractItemModel::removeRows ().
[override virtual]
int
QStandardItemModel::
rowCount
(const
QModelIndex
&
parent
= QModelIndex()) const
重實現自 QAbstractItemModel::rowCount ().
另請參閱 setRowCount ().
Sets the number of columns in this model to columns 。若這小於 columnCount (),不想要列中的數據被丟棄。
該函數在 Qt 4.2 引入。
另請參閱 columnCount () 和 setRowCount ().
[override virtual]
bool
QStandardItemModel::
setData
(const
QModelIndex
&
index
, const
QVariant
&
value
,
int
role
= Qt::EditRole)
重實現自 QAbstractItemModel::setData ().
另請參閱 data ().
[override virtual]
bool
QStandardItemModel::
setHeaderData
(
int
section
,
Qt::Orientation
orientation
, const
QVariant
&
value
,
int
role
= Qt::EditRole)
重實現自 QAbstractItemModel::setHeaderData ().
另請參閱 headerData ().
Sets the horizontal header item for column to 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.
該函數在 Qt 4.2 引入。
另請參閱 horizontalHeaderItem (), setHorizontalHeaderLabels (),和 setVerticalHeaderItem ().
設置水平 Header (頭部) 標簽使用 labels . If necessary, the column count is increased to the size of labels .
該函數在 Qt 4.2 引入。
另請參閱 setHorizontalHeaderItem ().
設置項為給定 row and column to 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.
該函數在 Qt 4.2 引入。
另請參閱 item ().
這是重載函數。
[override virtual]
bool
QStandardItemModel::
setItemData
(const
QModelIndex
&
index
, const
QMap
<
int
,
QVariant
> &
roles
)
重實現自 QAbstractItemModel::setItemData ().
另請參閱 itemData ().
Sets the item prototype for the model to the specified item . The model takes ownership of the prototype.
The item prototype acts as a QStandardItem factory, by relying on the QStandardItem::clone () function. To provide your own prototype, subclass QStandardItem , reimplement QStandardItem::clone () and set the prototype to be an instance of your custom class. Whenever QStandardItemModel needs to create an item on demand (for instance, when a view or item delegate calls setData ())), the new items will be instances of your custom class.
該函數在 Qt 4.2 引入。
另請參閱 itemPrototype () 和 QStandardItem::clone ().
Sets the item role names to roleNames .
Sets the number of rows in this model to rows 。若這小於 rowCount (),丟棄不想要行中的數據。
該函數在 Qt 4.2 引入。
另請參閱 rowCount () 和 setColumnCount ().
Sets the vertical header item for row to 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.
該函數在 Qt 4.2 引入。
另請參閱 verticalHeaderItem (), setVerticalHeaderLabels (),和 setHorizontalHeaderItem ().
設置垂直 Header (頭部) 標簽使用 labels . If necessary, the row count is increased to the size of labels .
該函數在 Qt 4.2 引入。
另請參閱 setVerticalHeaderItem ().
[override virtual]
QModelIndex
QStandardItemModel::
sibling
(
int
row
,
int
column
, const
QModelIndex
&
idx
) const
重實現自 QAbstractItemModel::sibling ().
[override virtual]
void
QStandardItemModel::
sort
(
int
column
,
Qt::SortOrder
order
= Qt::AscendingOrder)
重實現自 QAbstractItemModel::sort ().
[override virtual]
Qt::DropActions
QStandardItemModel::
supportedDropActions
() const
重實現自 QAbstractItemModel::supportedDropActions ().
QStandardItemModel supports both copy and move.
移除給定 column without deleting the column items, and returns a list of pointers to the removed items. The model releases ownership of the items. For items in the column that have not been set, the corresponding pointers in the list will be 0.
該函數在 Qt 4.2 引入。
另請參閱 takeRow ().
移除水平 Header (頭部) 項在 column from the header without deleting it, and returns a pointer to the item. The model releases ownership of the item.
該函數在 Qt 4.2 引入。
另請參閱 horizontalHeaderItem () 和 takeVerticalHeaderItem ().
Removes the item at ( row , column ) without deleting it. The model releases ownership of the item.
該函數在 Qt 4.2 引入。
另請參閱 item (), takeRow (),和 takeColumn ().
移除給定 row without deleting the row items, and returns a list of pointers to the removed items. The model releases ownership of the items. For items in the row that have not been set, the corresponding pointers in the list will be 0.
該函數在 Qt 4.2 引入。
另請參閱 takeColumn ().
移除垂直 Header (頭部) 項在 row from the header without deleting it, and returns a pointer to the item. The model releases ownership of the item.
該函數在 Qt 4.2 引入。
另請參閱 verticalHeaderItem () 和 takeHorizontalHeaderItem ().
返迴垂直 Header (頭部) 項為行
row
若有設置;否則返迴
nullptr
.
該函數在 Qt 4.2 引入。
另請參閱 setVerticalHeaderItem () 和 horizontalHeaderItem ().