QDataWidgetMapper

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

The PySide.QtGui.QDataWidgetMapper class provides mapping between a section of a data model to widgets.

PySide.QtGui.QDataWidgetMapper can 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 property is used to transfer data between the model and the widget. Since Qt 4.3, an additional PySide.QtGui.QDataWidgetMapper.addMapping() 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 PySide.QtGui.QItemDelegate is used to synchronize the model with the widgets.

Let us assume that we have an item model named model with 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 , myLineEdit and myCountryChooser :

mapper = QDataWidgetMapper
mapper.setModel(model)
mapper.addMapping(mySpinBox, 0)
mapper.addMapping(myLineEdit, 1)
mapper.addMapping(myCountryChooser, 2)
mapper.toFirst()

After the call to PySide.QtGui.QDataWidgetMapper.toFirst() , mySpinBox displays the value 1 , myLineEdit displays Qt Norway and myCountryChooser displays Oslo . The navigational functions PySide.QtGui.QDataWidgetMapper.toFirst() , PySide.QtGui.QDataWidgetMapper.toNext() , PySide.QtGui.QDataWidgetMapper.toPrevious() , PySide.QtGui.QDataWidgetMapper.toLast() and PySide.QtGui.QDataWidgetMapper.setCurrentIndex() can be used to navigate in the model and update the widgets with contents from the model.

The PySide.QtGui.QDataWidgetMapper.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.

PySide.QtGui.QDataWidgetMapper supports two submit policies, AutoSubmit and ManualSubmit . AutoSubmit will update the model as soon as the current widget loses focus, ManualSubmit will not update the model unless PySide.QtGui.QDataWidgetMapper.submit() is called. ManualSubmit is 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.

Note that PySide.QtGui.QDataWidgetMapper keeps track of external modifications. If the contents of the model are updated in another module of the application, the widgets are updated as well.

class PySide.QtGui.QDataWidgetMapper([parent=None])
Parameters:parentPySide.QtCore.QObject

Constructs a new PySide.QtGui.QDataWidgetMapper with parent object parent . By default, the orientation is horizontal and the submit policy is AutoSubmit .

PySide.QtGui.QDataWidgetMapper.SubmitPolicy

This enum describes the possible submit policies a PySide.QtGui.QDataWidgetMapper supports.

Constant Description
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 PySide.QtGui.QDataWidgetMapper.submit() is called.
PySide.QtGui.QDataWidgetMapper.addMapping(widget, section)
Parameters:

Adds a mapping between a widget and a 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 PySide.QtGui.QLineEdit nameLineEdit , and the second is mapped to the PySide.QtGui.QSpinBox ageSpinBox :

mapper = QDataWidgetMapper()
mapper.setModel(myModel)
mapper.addMapping(nameLineEdit, 0)
mapper.addMapping(ageSpinBox, 1)

Notes:

  • If the 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.
PySide.QtGui.QDataWidgetMapper.addMapping(widget, section, propertyName)
Parameters:

Essentially the same as PySide.QtGui.QDataWidgetMapper.addMapping() , but adds the possibility to specify the property to use specifying propertyName .

PySide.QtGui.QDataWidgetMapper.clearMapping()

Clears all mappings.

PySide.QtGui.QDataWidgetMapper.currentIndex()
Return type:PySide.QtCore.int

This property holds the current row or column.

The widgets are populated with with data from the row at index if the orientation is horizontal (the default), otherwise with data from the column at index .

PySide.QtGui.QDataWidgetMapper.currentIndexChanged(index)
Parameters:indexPySide.QtCore.int
PySide.QtGui.QDataWidgetMapper.itemDelegate()
Return type:PySide.QtGui.QAbstractItemDelegate

Returns the current item delegate.

PySide.QtGui.QDataWidgetMapper.mappedPropertyName(widget)
Parameters:widgetPySide.QtGui.QWidget
Return type:PySide.QtCore.QByteArray

Returns the name of the property that is used when mapping data to the given widget .

PySide.QtGui.QDataWidgetMapper.mappedSection(widget)
Parameters:widgetPySide.QtGui.QWidget
Return type:PySide.QtCore.int

Returns the section the widget is mapped to or -1 if the widget is not mapped.

PySide.QtGui.QDataWidgetMapper.mappedWidgetAt(section)
Parameters:sectionPySide.QtCore.int
Return type:PySide.QtGui.QWidget

Returns the widget that is mapped at section , or 0 if no widget is mapped at that section.

PySide.QtGui.QDataWidgetMapper.model()
Return type:PySide.QtCore.QAbstractItemModel

Returns the current model.

PySide.QtGui.QDataWidgetMapper.orientation()
Return type:PySide.QtCore.Qt.Orientation

This property holds the orientation of the model.

If the orientation is Qt.Horizontal (the default), a widget is mapped to a column of a data model. The widget will be populated with the model’s data from its mapped column and the row that PySide.QtGui.QDataWidgetMapper.currentIndex() points at.

Use Qt.Horizontal for tabular data that looks like this:

1 Qt Norway Oslo
2 Qt Australia Brisbane
3 Qt USA Silicon Valley
4 Qt China Beijing
5 Qt Germany Berlin

If the orientation is set to Qt.Vertical , a widget is mapped to a row. Calling PySide.QtGui.QDataWidgetMapper.setCurrentIndex() will change the current column. The widget will be populates with the model’s data from its mapped row and the column that PySide.QtGui.QDataWidgetMapper.currentIndex() points at.

Use Qt.Vertical for tabular data that looks like this:

1 2 3 4 5
Qt Norway Qt Australia Qt USA Qt China Qt Germany
Oslo Brisbane Silicon Valley Beijing Berlin

Changing the orientation clears all existing mappings.

PySide.QtGui.QDataWidgetMapper.removeMapping(widget)
Parameters:widgetPySide.QtGui.QWidget

Removes the mapping for the given widget .

PySide.QtGui.QDataWidgetMapper.revert()

Repopulates all widgets with the current data of the model. All unsubmitted changes will be lost.

PySide.QtGui.QDataWidgetMapper.rootIndex()
Return type:PySide.QtCore.QModelIndex

Returns the current root index.

PySide.QtGui.QDataWidgetMapper.setCurrentIndex(index)
Parameters:indexPySide.QtCore.int

This property holds the current row or column.

The widgets are populated with with data from the row at index if the orientation is horizontal (the default), otherwise with data from the column at index .

PySide.QtGui.QDataWidgetMapper.setCurrentModelIndex(index)
Parameters:indexPySide.QtCore.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 .

Calls PySide.QtGui.QDataWidgetMapper.setCurrentIndex() internally. This convenience slot can be connected to the signal PySide.QtGui.QItemSelectionModel.currentRowChanged() or PySide.QtGui.QItemSelectionModel.currentColumnChanged() of another view’s selection model .

The following example illustrates how to update all widgets with new data whenever the selection of a PySide.QtGui.QTableView named myTableView changes:

mapper = QDataWidgetMapper()
connect(myTableView.selectionModel(), SIGNAL("currentRowChanged(QModelIndex,QModelIndex)"),
        mapper, SLOT(setCurrentModelIndex(QModelIndex)))
PySide.QtGui.QDataWidgetMapper.setItemDelegate(delegate)
Parameters:delegatePySide.QtGui.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 QAbstractItemDelegate.setEditorData() and QAbstractItemDelegate.setModelData() .

The delegate also decides when to apply data and when to change the editor, using QAbstractItemDelegate.commitData() and QAbstractItemDelegate.closeEditor() .

Warning

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 PySide.QtGui.QAbstractItemDelegate.closeEditor() signal, and attempt to access, modify or close an editor that has already been closed.

PySide.QtGui.QDataWidgetMapper.setModel(model)
Parameters:modelPySide.QtCore.QAbstractItemModel

Sets the current model to model . If another model was set, all mappings to that old model are cleared.

PySide.QtGui.QDataWidgetMapper.setOrientation(aOrientation)
Parameters:aOrientationPySide.QtCore.Qt.Orientation

This property holds the orientation of the model.

If the orientation is Qt.Horizontal (the default), a widget is mapped to a column of a data model. The widget will be populated with the model’s data from its mapped column and the row that PySide.QtGui.QDataWidgetMapper.currentIndex() points at.

Use Qt.Horizontal for tabular data that looks like this:

1 Qt Norway Oslo
2 Qt Australia Brisbane
3 Qt USA Silicon Valley
4 Qt China Beijing
5 Qt Germany Berlin

If the orientation is set to Qt.Vertical , a widget is mapped to a row. Calling PySide.QtGui.QDataWidgetMapper.setCurrentIndex() will change the current column. The widget will be populates with the model’s data from its mapped row and the column that PySide.QtGui.QDataWidgetMapper.currentIndex() points at.

Use Qt.Vertical for tabular data that looks like this:

1 2 3 4 5
Qt Norway Qt Australia Qt USA Qt China Qt Germany
Oslo Brisbane Silicon Valley Beijing Berlin

Changing the orientation clears all existing mappings.

PySide.QtGui.QDataWidgetMapper.setRootIndex(index)
Parameters:indexPySide.QtCore.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.

PySide.QtGui.QDataWidgetMapper.setSubmitPolicy(policy)
Parameters:policyPySide.QtGui.QDataWidgetMapper.SubmitPolicy

This property holds the current submit policy.

Changing the current submit policy will revert all widgets to the current data from the model.

PySide.QtGui.QDataWidgetMapper.submit()
Return type:PySide.QtCore.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 PySide.QtCore.QAbstractItemModel.submit() method is invoked.

Returns true if all the values were submitted, otherwise false.

Note: For database models, QSqlQueryModel.lastError() can be used to retrieve the last error.

PySide.QtGui.QDataWidgetMapper.submitPolicy()
Return type:PySide.QtGui.QDataWidgetMapper.SubmitPolicy

This property holds the current submit policy.

Changing the current submit policy will revert all widgets to the current data from the model.

PySide.QtGui.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.

This is equivalent to calling setCurrentIndex(0) .

PySide.QtGui.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.

Calls PySide.QtGui.QDataWidgetMapper.setCurrentIndex() internally.

PySide.QtGui.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.

Calls PySide.QtGui.QDataWidgetMapper.setCurrentIndex() internally. Does nothing if there is no next row in the model.

PySide.QtGui.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.

Calls PySide.QtGui.QDataWidgetMapper.setCurrentIndex() internally. Does nothing if there is no previous row in the model.