PySide.QtGui.QStandardItemModel class provides a generic model for storing custom data.
PySide.QtGui.QStandardItemModel can be used as a repository for standard Qt data types. It is one of the 模型/视图类 且属于 Qt 的 模型/视图 框架。
PySide.QtGui.QStandardItemModel provides a classic item-based approach to working with the model. The items in a PySide.QtGui.QStandardItemModel are provided by PySide.QtGui.QStandardItem .
PySide.QtGui.QStandardItemModel 实现 PySide.QtCore.QAbstractItemModel interface, which means that the model can be used to provide data in any view that supports that interface (such as PySide.QtGui.QListView , PySide.QtGui.QTableView and PySide.QtGui.QTreeView , and your own custom views). For performance and flexibility, you may want to subclass PySide.QtCore.QAbstractItemModel to provide support for different kinds of data repositories. For example, the PySide.QtGui.QDirModel provides a model interface to the underlying file system.
When you want a list or tree, you typically create an empty PySide.QtGui.QStandardItemModel and use PySide.QtGui.QStandardItemModel.appendRow() to add items to the model, and PySide.QtGui.QStandardItemModel.item() to access an item. If your model represents a table, you typically pass the dimensions of the table to the PySide.QtGui.QStandardItemModel constructor and use PySide.QtGui.QStandardItemModel.setItem() to position items into the table. You can also use PySide.QtGui.QStandardItemModel.setRowCount() and PySide.QtGui.QStandardItemModel.setColumnCount() to alter the dimensions of the model. To insert items, use PySide.QtGui.QStandardItemModel.insertRow() or PySide.QtGui.QStandardItemModel.insertColumn() , and to remove items, use PySide.QtCore.QAbstractItemModel.removeRow() or PySide.QtCore.QAbstractItemModel.removeColumn() .
You can set the header labels of your model with PySide.QtGui.QStandardItemModel.setHorizontalHeaderLabels() and PySide.QtGui.QStandardItemModel.setVerticalHeaderLabels() .
You can search for items in the model with PySide.QtGui.QStandardItemModel.findItems() , and sort the model by calling PySide.QtGui.QStandardItemModel.sort() .
调用 PySide.QtGui.QStandardItemModel.clear() to remove all items from the model.
An example usage of PySide.QtGui.QStandardItemModel to create a table:
model = QStandardItemModel (4, 4)
for row in range(4):
for column in range(4):
item = QStandardItem("row %d, column %d" % (row, column))
model.setItem(row, column, item)
An example usage of PySide.QtGui.QStandardItemModel to create a tree:
model = QStandardItemModel()
parentItem = model.invisibleRootItem()
for i in range(4):
item = QStandardItem("item %d" % 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 PySide.QtGui.QAbstractItemView 提供 PySide.QtCore.QModelIndex -based signals and functions, you need a way to obtain the PySide.QtGui.QStandardItem that corresponds to a given PySide.QtCore.QModelIndex , and vice versa. PySide.QtGui.QStandardItemModel.itemFromIndex() and PySide.QtGui.QStandardItemModel.indexFromItem() provide this mapping. Typical usage of PySide.QtGui.QStandardItemModel.itemFromIndex() includes obtaining the item at the current index in a view, and obtaining the item that corresponds to an index carried by a PySide.QtGui.QAbstractItemView signal, such as QAbstractItemView.clicked() . First you connect the view's signal to a slot in your class:
treeView = QTreeView(self)
treeView.setModel(myStandardItemModel)
treeView.clicked[QModelIndex].connect(self.clicked)
When you receive the signal, you call PySide.QtGui.QStandardItemModel.itemFromIndex() on the given model index to get a pointer to the item:
def clicked(self, index):
item = myStandardItemModel.itemFromIndex(index)
# Do stuff with the item ...
Conversely, you must obtain the PySide.QtCore.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 PySide.QtGui.QStandardItemModel.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 PySide.QtCore.QAbstractItemModel interface when working with the model, or use a combination of the two as appropriate.
另请参阅
PySide.QtGui.QStandardItem 模型/视图编程 PySide.QtCore.QAbstractItemModel Simple Tree Model example 项视图方便类
| 参数: |
|
|---|
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 .
| 参数: | items – |
|---|
| 参数: | items – |
|---|
| 参数: | item – PySide.QtGui.QStandardItem |
|---|
这是重载函数。
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 all items (including header items) from the model and sets the number of rows and columns to zero.
另请参阅
PySide.QtGui.QStandardItemModel.removeColumns() PySide.QtGui.QStandardItemModel.removeRows()
| 参数: |
|
|---|---|
| 返回类型: |
| 参数: | column – PySide.QtCore.int |
|---|---|
| 返回类型: | PySide.QtGui.QStandardItem |
Returns the horizontal header item for column if one has been set; otherwise returns 0.
| 参数: | item – PySide.QtGui.QStandardItem |
|---|---|
| 返回类型: | PySide.QtCore.QModelIndex |
返回 PySide.QtCore.QModelIndex 关联给定 item .
Use this function when you want to perform an operation that requires the PySide.QtCore.QModelIndex of the item, such as QAbstractItemView.scrollTo() . QStandardItem.index() is provided as convenience; it is equivalent to calling this function.
| 参数: |
|
|---|
| 参数: |
|
|---|
| 参数: |
|
|---|
这是重载函数。
Inserts a row at row containing 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.
| 返回类型: | PySide.QtGui.QStandardItem |
|---|
Returns the model's invisible root item.
The invisible root item provides access to the model's top-level items through the PySide.QtGui.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.
注意
调用 PySide.QtCore.QAbstractItemModel.index() 在 PySide.QtGui.QStandardItem object retrieved from this function is not valid.
| 参数: |
|
|---|---|
| 返回类型: |
返回项为给定 row and column if one has been set; otherwise returns 0.
| 参数: | item – PySide.QtGui.QStandardItem |
|---|
| 参数: | index – PySide.QtCore.QModelIndex |
|---|---|
| 返回类型: | PySide.QtGui.QStandardItem |
返回指针指向 PySide.QtGui.QStandardItem 关联给定 index .
Calling this function is typically the initial step when processing PySide.QtCore.QModelIndex -based signals from a view, such as QAbstractItemView.activated() . In your slot, you call PySide.QtGui.QStandardItemModel.itemFromIndex() , with the PySide.QtCore.QModelIndex carried by the signal as argument, to obtain a pointer to the corresponding PySide.QtGui.QStandardItem .
Note that this function will lazily create an item for the index (using PySide.QtGui.QStandardItemModel.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 0.
| 返回类型: | PySide.QtGui.QStandardItem |
|---|
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 PySide.QtGui.QStandardItemModel.setData() ).
| 参数: | columns – PySide.QtCore.int |
|---|
Sets the number of columns in this model to columns . If this is less than PySide.QtGui.QStandardItemModel.columnCount() , the data in the unwanted columns is discarded.
另请参阅
PySide.QtGui.QStandardItemModel.columnCount() PySide.QtGui.QStandardItemModel.setRowCount()
| 参数: |
|
|---|
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.
| 参数: | labels – list of strings |
|---|
设置水平 Header 头标签,使用 labels . If necessary, the column count is increased to the size of labels .
| 参数: |
|
|---|
Sets the item for the given 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.
| 参数: |
|
|---|
这是重载函数。
| 参数: | item – PySide.QtGui.QStandardItem |
|---|
Sets the item prototype for the model to the specified item . The model takes ownership of the prototype.
The item prototype acts as a PySide.QtGui.QStandardItem factory, by relying on the QStandardItem.clone() function. To provide your own prototype, subclass PySide.QtGui.QStandardItem , reimplement QStandardItem.clone() and set the prototype to be an instance of your custom class. Whenever PySide.QtGui.QStandardItemModel needs to create an item on demand (for instance, when a view or item delegate calls PySide.QtGui.QStandardItemModel.setData() )), the new items will be instances of your custom class.
| 参数: | rows – PySide.QtCore.int |
|---|
Sets the number of rows in this model to rows . If this is less than PySide.QtGui.QStandardItemModel.rowCount() , the data in the unwanted rows is discarded.
另请参阅
PySide.QtGui.QStandardItemModel.rowCount() PySide.QtGui.QStandardItemModel.setColumnCount()
| 参数: | role – PySide.QtCore.int |
|---|
This property holds the item role that is used to query the model's data when sorting items.
默认值为 Qt.DisplayRole .
另请参阅
PySide.QtGui.QStandardItemModel.sort() QStandardItem.sortChildren()
| 参数: |
|
|---|
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.
| 参数: | labels – list of strings |
|---|
Sets the vertical header labels using labels . If necessary, the row count is increased to the size of labels .
| 返回类型: | PySide.QtCore.int |
|---|
This property holds the item role that is used to query the model's data when sorting items.
默认值为 Qt.DisplayRole .
另请参阅
PySide.QtGui.QStandardItemModel.sort() QStandardItem.sortChildren()
| 参数: | column – PySide.QtCore.int |
|---|---|
| 返回类型: |
移除给定 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.
| 参数: | column – PySide.QtCore.int |
|---|---|
| 返回类型: | PySide.QtGui.QStandardItem |
Removes the horizontal header item at column from the header without deleting it, and returns a pointer to the item. The model releases ownership of the item.
| 参数: |
|
|---|---|
| 返回类型: |
Removes the item at ( row , column ) without deleting it. The model releases ownership of the item.
| 参数: | row – PySide.QtCore.int |
|---|---|
| 返回类型: |
移除给定 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.
| 参数: | row – PySide.QtCore.int |
|---|---|
| 返回类型: | PySide.QtGui.QStandardItem |
移除垂直 Header 头部项在 row from the header without deleting it, and returns a pointer to the item. The model releases ownership of the item.
| 参数: | row – PySide.QtCore.int |
|---|---|
| 返回类型: | PySide.QtGui.QStandardItem |
Returns the vertical header item for row row if one has been set; otherwise returns 0.