QDataWidgetMapperclass provides mapping between a section of a data model to widgets. 更多 …
def
addMapping
(widget, section)
def
addMapping
(widget, section, propertyName)
def
clearMapping
()
def
currentIndex
()
def
itemDelegate
()
def
mappedPropertyName
(widget)
def
mappedSection
(widget)
def
mappedWidgetAt
(section)
def
model
()
def
orientation
()
def
removeMapping
(widget)
def
rootIndex
()
def
setItemDelegate
(delegate)
def
setModel
(model)
def
setOrientation
(aOrientation)
def
setRootIndex
(index)
def
setSubmitPolicy
(policy)
def
submitPolicy
()
def
setCurrentIndex
(index)
def
revert
()
def
setCurrentModelIndex
(index)
def
submit
()
def
toFirst
()
def
toLast
()
def
toNext
()
def
toPrevious
()
def
currentIndexChanged
(index)
QDataWidgetMappercan be used to create data-aware widgets by mapping them to sections of an item model. A section is a column of a model if the orientation is horizontal (the default), otherwise a row.Every time the current index changes, each widget is updated with data from the model via the property specified when its mapping was made. If the user edits the contents of a widget, the changes are read using the same property and written back to the model. By default, each widget’s
user 特性is used to transfer data between the model and the widget. Since Qt 4.3, an additionaladdMapping()function enables a named property to be used instead of the default user property.It is possible to set an item delegate to support custom widgets. By default, a
QItemDelegateis used to synchronize the model with the widgets.Let us assume that we have an item model named
modelwith the following contents:
1
Qt Norway
Oslo
2
Qt Australia
Brisbane
3
Qt USA
Palo Alto
4
Qt China
Beijing
5
Qt Germany
Berlin
The following code will map the columns of the model to widgets called
mySpinBox,myLineEditandmyCountryChooser:mapper = QDataWidgetMapper mapper.setModel(model) mapper.addMapping(mySpinBox, 0) mapper.addMapping(myLineEdit, 1) mapper.addMapping(myCountryChooser, 2) mapper.toFirst()After the call to
toFirst(),mySpinBoxdisplays the value1,myLineEditdisplaysQt NorwayandmyCountryChooserdisplaysOslo. The navigational functionstoFirst(),toNext(),toPrevious(),toLast()andsetCurrentIndex()can be used to navigate in the model and update the widgets with contents from the model.
setRootIndex()function enables a particular item in a model to be specified as the root index - children of this item will be mapped to the relevant widgets in the user interface.
QDataWidgetMappersupports two submit policies,AutoSubmitandManualSubmit.AutoSubmitwill update the model as soon as the current widget loses focus,ManualSubmitwill not update the model unlesssubmit()被调用。ManualSubmitis useful when displaying a dialog that lets the user cancel all modifications. Also, other views that display the model won’t update until the user finishes all their modifications and submits.注意:
QDataWidgetMapperkeeps track of external modifications. If the contents of the model are updated in another module of the application, the widgets are updated as well.
QDataWidgetMapper
(
[
parent=None
]
)
¶
- param parent
QObject
构造新
QDataWidgetMapper
with parent object
parent
. By default, the orientation is horizontal and the submit policy is
AutoSubmit
.
PySide2.QtWidgets.QDataWidgetMapper.
SubmitPolicy
¶
This enum describes the possible submit policies a
QDataWidgetMapper
支持。
|
常量 |
描述 |
|---|---|
|
QDataWidgetMapper.AutoSubmit |
Whenever a widget loses focus, the widget’s current value is set to the item model. |
|
QDataWidgetMapper.ManualSubmit |
The model is not updated until
|
PySide2.QtWidgets.QDataWidgetMapper.
addMapping
(
widget
,
section
)
¶
widget
–
QWidget
section
–
int
Adds a mapping between a
widget
和
section
from the model. The
section
is a column in the model if the orientation is horizontal (the default), otherwise a row.
For the following example, we assume a model
myModel
that has two columns: the first one contains the names of people in a group, and the second column contains their ages. The first column is mapped to the
QLineEdit
nameLineEdit
, and the second is mapped to the
QSpinBox
ageSpinBox
:
mapper = QDataWidgetMapper()
mapper.setModel(myModel)
mapper.addMapping(nameLineEdit, 0)
mapper.addMapping(ageSpinBox, 1)
注意事项:
若
widget
is already mapped to a section, the old mapping will be replaced by the new one.
Only one-to-one mappings between sections and widgets are allowed. It is not possible to map a single section to multiple widgets, or to map a single widget to multiple sections.
PySide2.QtWidgets.QDataWidgetMapper.
addMapping
(
widget
,
section
,
propertyName
)
¶
widget
–
QWidget
section
–
int
propertyName
–
QByteArray
Essentially the same as
addMapping()
, but adds the possibility to specify the property to use specifying
propertyName
.
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
clearMapping
(
)
¶
Clears all mappings.
PySide2.QtWidgets.QDataWidgetMapper.
currentIndex
(
)
¶
int
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
currentIndexChanged
(
index
)
¶
index
–
int
PySide2.QtWidgets.QDataWidgetMapper.
itemDelegate
(
)
¶
Returns the current item delegate.
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
mappedPropertyName
(
widget
)
¶
widget
–
QWidget
QByteArray
Returns the name of the property that is used when mapping data to the given
widget
.
PySide2.QtWidgets.QDataWidgetMapper.
mappedSection
(
widget
)
¶
widget
–
QWidget
int
Returns the section the
widget
is mapped to or -1 if the widget is not mapped.
PySide2.QtWidgets.QDataWidgetMapper.
mappedWidgetAt
(
section
)
¶
section
–
int
Returns the widget that is mapped at
section
, or 0 if no widget is mapped at that section.
PySide2.QtWidgets.QDataWidgetMapper.
model
(
)
¶
QAbstractItemModel
Returns the current model.
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
orientation
(
)
¶
取向
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
removeMapping
(
widget
)
¶
widget
–
QWidget
Removes the mapping for the given
widget
.
PySide2.QtWidgets.QDataWidgetMapper.
revert
(
)
¶
Repopulates all widgets with the current data of the model. All unsubmitted changes will be lost.
PySide2.QtWidgets.QDataWidgetMapper.
rootIndex
(
)
¶
QModelIndex
Returns the current root index.
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
setCurrentIndex
(
index
)
¶
index
–
int
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
setCurrentModelIndex
(
index
)
¶
index
–
QModelIndex
Sets the current index to the row of the
index
if the orientation is horizontal (the default), otherwise to the column of the
index
.
调用
setCurrentIndex()
internally. This convenience slot can be connected to the signal
currentRowChanged()
or
currentColumnChanged()
of another view’s
selection
model
.
The following example illustrates how to update all widgets with new data whenever the selection of a
QTableView
named
myTableView
changes:
mapper = QDataWidgetMapper()
connect(myTableView.selectionModel(), SIGNAL("currentRowChanged(QModelIndex,QModelIndex)"),
mapper, SLOT(setCurrentModelIndex(QModelIndex)))
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
setItemDelegate
(
delegate
)
¶
delegate
–
QAbstractItemDelegate
Sets the item delegate to
delegate
. The delegate will be used to write data from the model into the widget and from the widget to the model, using
setEditorData()
and
setModelData()
.
The delegate also decides when to apply data and when to change the editor, using
commitData()
and
closeEditor()
.
警告
You should not share the same instance of a delegate between widget mappers or views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the
closeEditor()
signal, and attempt to access, modify or close an editor that has already been closed.
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
setModel
(
model
)
¶
model
–
QAbstractItemModel
Sets the current model to
model
. If another model was set, all mappings to that old model are cleared.
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
setOrientation
(
aOrientation
)
¶
aOrientation
–
取向
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
setRootIndex
(
index
)
¶
index
–
QModelIndex
Sets the root item to
index
. This can be used to display a branch of a tree. Pass an invalid model index to display the top-most branch.
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
setSubmitPolicy
(
policy
)
¶
policy
–
SubmitPolicy
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
submit
(
)
¶
bool
Submits all changes from the mapped widgets to the model.
For every mapped section, the item delegate reads the current value from the widget and sets it in the model. Finally, the model’s
submit()
method is invoked.
返回
true
if all the values were submitted, otherwise false.
Note: For database models,
lastError()
can be used to retrieve the last error.
PySide2.QtWidgets.QDataWidgetMapper.
submitPolicy
(
)
¶
另请参阅
PySide2.QtWidgets.QDataWidgetMapper.
toFirst
(
)
¶
Populates the widgets with data from the first row of the model if the orientation is horizontal (the default), otherwise with data from the first column.
这相当于调用
setCurrentIndex(0)
.
PySide2.QtWidgets.QDataWidgetMapper.
toLast
(
)
¶
Populates the widgets with data from the last row of the model if the orientation is horizontal (the default), otherwise with data from the last column.
调用
setCurrentIndex()
internally.
PySide2.QtWidgets.QDataWidgetMapper.
toNext
(
)
¶
Populates the widgets with data from the next row of the model if the orientation is horizontal (the default), otherwise with data from the next column.
调用
setCurrentIndex()
internally. Does nothing if there is no next row in the model.
PySide2.QtWidgets.QDataWidgetMapper.
toPrevious
(
)
¶
Populates the widgets with data from the previous row of the model if the orientation is horizontal (the default), otherwise with data from the previous column.
调用
setCurrentIndex()
internally. Does nothing if there is no previous row in the model.