QStandardItemModelclass provides a generic model for storing custom data. 更多 …
def
appendColumn
(items)
def
appendRow
(item)
def
appendRow
(items)
def
clear
()
def
clearItemData
(index)
def
findItems
(text[, flags=Qt.MatchExactly[, column=0]])
def
horizontalHeaderItem
(column)
def
indexFromItem
(item)
def
insertColumn
(column, items)
def
insertRow
(row, item)
def
insertRow
(row, items)
def
invisibleRootItem
()
def
item
(row[, column=0])
def
itemFromIndex
(index)
def
itemPrototype
()
def
setColumnCount
(columns)
def
setHorizontalHeaderItem
(column, item)
def
setHorizontalHeaderLabels
(labels)
def
setItem
(row, column, item)
def
setItem
(row, item)
def
setItemPrototype
(item)
def
setItemRoleNames
(roleNames)
def
setRowCount
(rows)
def
setSortRole
(role)
def
setVerticalHeaderItem
(row, item)
def
setVerticalHeaderLabels
(labels)
def
sortRole
()
def
takeColumn
(column)
def
takeHorizontalHeaderItem
(column)
def
takeItem
(row[, column=0])
def
takeRow
(row)
def
takeVerticalHeaderItem
(row)
def
verticalHeaderItem
(row)
def
itemChanged
(item)
QStandardItemModelcan be used as a repository for standard Qt data types. It is one of the 模型/视图类 and is part of Qt’s 模型/视图 框架。
QStandardItemModelprovides a classic item-based approach to working with the model. The items in aQStandardItemModelare provided byQStandardItem.
QStandardItemModel实现QAbstractItemModelinterface, which means that the model can be used to provide data in any view that supports that interface (such asQListView,QTableViewandQTreeView, and your own custom views). For performance and flexibility, you may want to subclassQAbstractItemModelto provide support for different kinds of data repositories. For example, theQDirModelprovides a model interface to the underlying file system.When you want a list or tree, you typically create an empty
QStandardItemModeland useappendRow()to add items to the model, anditem()to access an item. If your model represents a table, you typically pass the dimensions of the table to theQStandardItemModelconstructor and usesetItem()to position items into the table. You can also usesetRowCount()andsetColumnCount()to alter the dimensions of the model. To insert items, useinsertRow()orinsertColumn(), and to remove items, useremoveRow()orremoveColumn().You can set the header labels of your model with
setHorizontalHeaderLabels()andsetVerticalHeaderLabels().You can search for items in the model with
findItems(), and sort the model by callingsort().调用
clear()to remove all items from the model.An example usage of
QStandardItemModelto 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
QStandardItemModelto create a tree:model = QStandardItemModel() parentItem = model.invisibleRootItem() for i in range(4): item = QStandardItem("item %d" % i) parentItem.appendRow(item) parentItem = itemAfter 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 theQStandardItemthat corresponds to a givenQModelIndex, and vice versa.itemFromIndex()andindexFromItem()provide this mapping. Typical usage ofitemFromIndex()includes obtaining the item at the current index in a view, and obtaining the item that corresponds to an index carried by aQAbstractItemViewsignal, such asclicked(). 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
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
QModelIndexof 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’sindexFromItem()function, or, equivalently, by callingindex():treeView.scrollTo(item.index())You are, of course, not required to use the item-based approach; you could instead rely entirely on the
QAbstractItemModelinterface when working with the model, or use a combination of the two as appropriate.
QStandardItemModel
(
[
parent=None
]
)
¶
QStandardItemModel(rows, columns[, parent=None])
- param parent
QObject- param columns
int- param rows
int
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
.
PySide2.QtGui.QStandardItemModel.
appendColumn
(
items
)
¶
items –
Appends a column containing
items
. If necessary, the row count is increased to the size of
items
.
PySide2.QtGui.QStandardItemModel.
appendRow
(
item
)
¶
item
–
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
.
PySide2.QtGui.QStandardItemModel.
appendRow
(
items
)
¶
items –
PySide2.QtGui.QStandardItemModel.
clear
(
)
¶
Removes all items (including header items) from the model and sets the number of rows and columns to zero.
另请参阅
removeColumns()
removeRows()
PySide2.QtGui.QStandardItemModel.
clearItemData
(
index
)
¶
index
–
QModelIndex
bool
Removes the data stored in all the roles for the given
index
。返回
true
if
index
is valid and data was cleared,
false
否则。
另请参阅
setData()
data()
PySide2.QtGui.QStandardItemModel.
findItems
(
text
[
,
flags=Qt.MatchExactly
[
,
column=0
]
]
)
¶
text – unicode
flags
–
MatchFlags
column
–
int
返回的项列表匹配给定
text
,使用给定
flags
,在给定
column
.
PySide2.QtGui.QStandardItemModel.
horizontalHeaderItem
(
column
)
¶
column
–
int
Returns the horizontal header item for
column
若有设置;否则返回
None
.
PySide2.QtGui.QStandardItemModel.
indexFromItem
(
item
)
¶
item
–
QStandardItem
QModelIndex
返回
QModelIndex
关联给定
item
.
Use this function when you want to perform an operation that requires the
QModelIndex
of the item, such as
scrollTo()
.
index()
is provided as convenience; it is equivalent to calling this function.
另请参阅
PySide2.QtGui.QStandardItemModel.
insertColumn
(
column
,
items
)
¶
column
–
int
items –
PySide2.QtGui.QStandardItemModel.
insertRow
(
row
,
item
)
¶
row
–
int
item
–
QStandardItem
这是重载函数。
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.
PySide2.QtGui.QStandardItemModel.
insertRow
(
row
,
items
)
¶
row
–
int
items –
PySide2.QtGui.QStandardItemModel.
invisibleRootItem
(
)
¶
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.
PySide2.QtGui.QStandardItemModel.
item
(
row
[
,
column=0
]
)
¶
row
–
int
column
–
int
返回项为给定
row
and
column
若有设置;否则返回
None
.
PySide2.QtGui.QStandardItemModel.
itemChanged
(
item
)
¶
item
–
QStandardItem
PySide2.QtGui.QStandardItemModel.
itemFromIndex
(
index
)
¶
index
–
QModelIndex
返回指针指向
QStandardItem
关联给定
index
.
Calling this function is typically the initial step when processing
QModelIndex
-based signals from a view, such as
activated()
. In your slot, you call , 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
None
.
另请参阅
PySide2.QtGui.QStandardItemModel.
itemPrototype
(
)
¶
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()
).
另请参阅
PySide2.QtGui.QStandardItemModel.
setColumnCount
(
columns
)
¶
columns
–
int
Sets the number of columns in this model to
columns
. If this is less than
columnCount()
, the data in the unwanted columns is discarded.
另请参阅
columnCount()
setRowCount()
PySide2.QtGui.QStandardItemModel.
setHorizontalHeaderItem
(
column
,
item
)
¶
column
–
int
item
–
QStandardItem
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.
PySide2.QtGui.QStandardItemModel.
setHorizontalHeaderLabels
(
labels
)
¶
labels – 字符串列表
设置水平 Header 头标签,使用
labels
. If necessary, the column count is increased to the size of
labels
.
PySide2.QtGui.QStandardItemModel.
setItem
(
row
,
item
)
¶
row
–
int
item
–
QStandardItem
这是重载函数。
PySide2.QtGui.QStandardItemModel.
setItem
(
row
,
column
,
item
)
¶
row
–
int
column
–
int
item
–
QStandardItem
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.
另请参阅
PySide2.QtGui.QStandardItemModel.
setItemPrototype
(
item
)
¶
item
–
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
QStandardItem
factory, by relying on the
clone()
function. To provide your own prototype, subclass
QStandardItem
, reimplement
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.
另请参阅
PySide2.QtGui.QStandardItemModel.
setItemRoleNames
(
roleNames
)
¶
roleNames –
Sets the item role names to
roleNames
.
PySide2.QtGui.QStandardItemModel.
setRowCount
(
rows
)
¶
rows
–
int
Sets the number of rows in this model to
rows
. If this is less than
rowCount()
, the data in the unwanted rows is discarded.
另请参阅
rowCount()
setColumnCount()
PySide2.QtGui.QStandardItemModel.
setSortRole
(
role
)
¶
role
–
int
另请参阅
PySide2.QtGui.QStandardItemModel.
setVerticalHeaderItem
(
row
,
item
)
¶
row
–
int
item
–
QStandardItem
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.
PySide2.QtGui.QStandardItemModel.
setVerticalHeaderLabels
(
labels
)
¶
labels – 字符串列表
Sets the vertical header labels using
labels
. If necessary, the row count is increased to the size of
labels
.
PySide2.QtGui.QStandardItemModel.
sortRole
(
)
¶
int
另请参阅
PySide2.QtGui.QStandardItemModel.
takeColumn
(
column
)
¶
column
–
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
None
.
另请参阅
PySide2.QtGui.QStandardItemModel.
takeHorizontalHeaderItem
(
column
)
¶
column
–
int
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.
PySide2.QtGui.QStandardItemModel.
takeItem
(
row
[
,
column=0
]
)
¶
row
–
int
column
–
int
Removes the item at (
row
,
column
) without deleting it. The model releases ownership of the item.
另请参阅
PySide2.QtGui.QStandardItemModel.
takeRow
(
row
)
¶
row
–
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
None
.
另请参阅
PySide2.QtGui.QStandardItemModel.
takeVerticalHeaderItem
(
row
)
¶
row
–
int
移除垂直 Header 头部项在
row
from the header without deleting it, and returns a pointer to the item. The model releases ownership of the item.
PySide2.QtGui.QStandardItemModel.
verticalHeaderItem
(
row
)
¶
row
–
int
Returns the vertical header item for row
row
若有设置;否则返回
None
.