Home · All Classes · Modules

QDataWidgetMapper Class Reference
[QtGui module]

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

Inherits QObject.

Types

Methods

Qt Signals


Detailed Description

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

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 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 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:

 QDataWidgetMapper *mapper = new QDataWidgetMapper;
 mapper->setModel(model);
 mapper->addMapping(mySpinBox, 0);
 mapper->addMapping(myLineEdit, 1);
 mapper->addMapping(myCountryChooser, 2);
 mapper->toFirst();

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

The 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.

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


Type Documentation

QDataWidgetMapper.SubmitPolicy

This enum describes the possible submit policies a QDataWidgetMapper supports.

Constant Value Description
QDataWidgetMapper.AutoSubmit 0 Whenever a widget loses focus, the widget's current value is set to the item model.
QDataWidgetMapper.ManualSubmit 1 The model is not updated until submit() is called.

Method Documentation

QDataWidgetMapper.__init__ (self, QObject parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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

See also setOrientation() and setSubmitPolicy().

QDataWidgetMapper.addMapping (self, QWidget widget, int section)

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 QLineEdit nameLineEdit, and the second is mapped to the QSpinBox ageSpinBox:

 QDataWidgetMapper *mapper = new QDataWidgetMapper();
 mapper->setModel(myModel);
 mapper->addMapping(nameLineEdit, 0);
 mapper->addMapping(ageSpinBox, 1);

Notes:

See also removeMapping(), mappedSection(), and clearMapping().

QDataWidgetMapper.addMapping (self, QWidget widget, int section, QByteArray propertyName)

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

This function was introduced in Qt 4.3.

See also addMapping().

QDataWidgetMapper.clearMapping (self)

Clears all mappings.

See also addMapping() and removeMapping().

int QDataWidgetMapper.currentIndex (self)

QAbstractItemDelegate QDataWidgetMapper.itemDelegate (self)

Returns the current item delegate.

See also setItemDelegate().

QByteArray QDataWidgetMapper.mappedPropertyName (self, QWidget widget)

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

This function was introduced in Qt 4.3.

See also mappedSection(), addMapping(), and removeMapping().

int QDataWidgetMapper.mappedSection (self, QWidget widget)

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

See also addMapping() and removeMapping().

QWidget QDataWidgetMapper.mappedWidgetAt (self, int section)

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

See also addMapping() and removeMapping().

QAbstractItemModel QDataWidgetMapper.model (self)

Returns the current model.

See also setModel().

Qt.Orientation QDataWidgetMapper.orientation (self)

QDataWidgetMapper.removeMapping (self, QWidget widget)

Removes the mapping for the given widget.

See also addMapping() and clearMapping().

QDataWidgetMapper.revert (self)

This method is also a Qt slot with the C++ signature void revert().

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

See also submit() and setSubmitPolicy().

QModelIndex QDataWidgetMapper.rootIndex (self)

Returns the current root index.

See also setRootIndex().

QDataWidgetMapper.setCurrentIndex (self, int index)

This method is also a Qt slot with the C++ signature void setCurrentIndex(int).

QDataWidgetMapper.setCurrentModelIndex (self, QModelIndex index)

This method is also a Qt slot with the C++ signature void setCurrentModelIndex(const 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 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:

 QDataWidgetMapper *mapper = new QDataWidgetMapper();
 connect(myTableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
         mapper, SLOT(setCurrentModelIndex(QModelIndex)));

See also currentIndex().

QDataWidgetMapper.setItemDelegate (self, QAbstractItemDelegate delegate)

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

See also itemDelegate().

QDataWidgetMapper.setModel (self, QAbstractItemModel model)

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

See also model().

QDataWidgetMapper.setOrientation (self, Qt.Orientation aOrientation)

QDataWidgetMapper.setRootIndex (self, QModelIndex index)

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.

See also rootIndex().

QDataWidgetMapper.setSubmitPolicy (self, SubmitPolicy policy)

bool QDataWidgetMapper.submit (self)

This method is also a Qt slot with the C++ signature bool submit().

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.

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

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

See also revert() and setSubmitPolicy().

SubmitPolicy QDataWidgetMapper.submitPolicy (self)

QDataWidgetMapper.toFirst (self)

This method is also a Qt slot with the C++ signature void 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).

See also toLast() and setCurrentIndex().

QDataWidgetMapper.toLast (self)

This method is also a Qt slot with the C++ signature void 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 setCurrentIndex() internally.

See also toFirst() and setCurrentIndex().

QDataWidgetMapper.toNext (self)

This method is also a Qt slot with the C++ signature void 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 setCurrentIndex() internally. Does nothing if there is no next row in the model.

See also toPrevious() and setCurrentIndex().

QDataWidgetMapper.toPrevious (self)

This method is also a Qt slot with the C++ signature void 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 setCurrentIndex() internally. Does nothing if there is no previous row in the model.

See also toNext() and setCurrentIndex().


Qt Signal Documentation

void currentIndexChanged (int)

This is the default overload of this signal.

This signal is emitted after the current index has changed and all widgets were populated with new data. index is the new current index.

See also currentIndex() and setCurrentIndex().


PyQt 4.10.1 for MacOSCopyright © Riverbank Computing Ltd and Nokia 2012Qt 4.8.4