PySide6.QtCore.QModelRoleDataSpan¶
- class QModelRoleDataSpan¶
The
QModelRoleDataSpanclass provides a span overQModelRoleDataobjects. More…Synopsis¶
Methods¶
def
__init__()def
__getitem__()def
__len__()def
begin()def
data()def
end()def
length()def
operator[]()def
size()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
A
QModelRoleDataSpanis used as an abstraction over an array ofQModelRoleDataobjects.Like a view,
QModelRoleDataSpanprovides a small object (pointer and size) that can be passed to functions that need to examine the contents of the array. AQModelRoleDataSpancan be constructed from any array-like sequence (plain arrays,QVector, std::vector,QVarLengthArray, and so on). Moreover, it does not own the sequence, which must therefore be kept alive longer than anyQModelRoleDataSpanobjects referencing it.Unlike a view,
QModelRoleDataSpanis a span, so it allows for modifications to the underlying elements.QModelRoleDataSpan‘s main use case is making it possible for a model to return the data corresponding to different roles in one call.In order to draw one element from a model, a view (through its delegates) will generally request multiple roles for the same index by calling
data()as many times as needed:text = model.data(index, Qt.ItemDataRole.DisplayRole) decoration = model.data(index, Qt.DecorationRole) checkState = model.data(index, Qt.CheckStateRole) # etc.
QModelRoleDataSpanallows a view to request the same data using just one function call.This is achieved by having the view prepare a suitable array of
QModelRoleDataobjects, each initialized with the role that should be fetched. The array is then wrapped in aQModelRoleDataSpanobject, which is then passed to a model’smultiData()function.std.array<QModelRoleData, 3> roleData = { { QModelRoleData(Qt.ItemDataRole.DisplayRole), QModelRoleData(Qt.DecorationRole), QModelRoleData(Qt.CheckStateRole) } } # Usually, this is not necessary: A QModelRoleDataSpan # will be built automatically for you when passing an array-like # container to multiData(). span = QModelRoleDataSpan(roleData) model.multiData(index, span) # Use roleData[0].data(), roleData[1].data(), etc.
Views are encouraged to store the array of
QModelRoleDataobjects (and, possibly, the corresponding span) and re-use it in subsequent calls to the model. This allows to reduce the memory allocations related with creating and returningQVariantobjects.Finally, given a
QModelRoleDataSpanobject, the model’s responsibility is to fill in the data corresponding to each role in the span. How this is done depends on the concrete model class. Here’s a sketch of a possible implementation that iterates over the span and usessetData()on each element:def multiData(self, index, roleDataSpan): for roleData in roleDataSpan: role = roleData.role() # ... obtain the data for index and role ... roleData.setData(result)
See also
- __init__()¶
Constructs an empty
QModelRoleDataSpan. Itsdata()will be set toNone, and its length to zero.- __init__(modelRoleData)
- Parameters:
modelRoleData –
QModelRoleData
Constructs an
QModelRoleDataSpanspanning overmodelRoleData, seen as a 1-element array.- __init__(modelRoleData, len)
- Parameters:
modelRoleData –
QModelRoleDatalen – int
Constructs an
QModelRoleDataSpanspanning over the array beginning atmodelRoleDataand with lengthlen.Note
The array must be kept alive as long as this object has not been destructed.
- __getitem__()¶
- __len__()¶
- Return type:
int
- begin()¶
- Return type:
Returns a pointer to the beginning of the span represented by this object.
- data()¶
- Return type:
Returns a pointer to the beginning of the span represented by this object.
- end()¶
- Return type:
Returns a pointer to the imaginary element one past the end of the span represented by this object.
- length()¶
- Return type:
int
Returns the length of the span represented by this object.
- operator(index)¶
- Parameters:
index – int
- Return type:
Returns a modifiable reference to the
QModelRoleDataat positionindexin the span.Note
indexmust be a valid index for this span (0 <=index<size()).- size()¶
- Return type:
int
Returns the length of the span represented by this object.