|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QDataWidgetMapper Class Reference
[QtGui module]

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

    #include <QDataWidgetMapper>

Inherits QObject.

This class was introduced in Qt 4.2.

- List of all members, including inherited members

### Public Types

- enum SubmitPolicy { AutoSubmit, ManualSubmit }

### Properties

- currentIndex : int
- orientation : Qt::Orientation
- submitPolicy : SubmitPolicy

- 1 property inherited from QObject

### Public Functions

- QDataWidgetMapper ( QObject * parent = 0 )
- ~QDataWidgetMapper ()
- void addMapping ( QWidget * widget, int section )
- void clearMapping ()
- int currentIndex () const
- QAbstractItemDelegate * itemDelegate () const
- int mappedSection ( QWidget * widget ) const
- QWidget * mappedWidgetAt ( int section ) const
- QAbstractItemModel * model () const
- Qt::Orientation orientation () const
- void removeMapping ( QWidget * widget )
- QModelIndex rootIndex () const
- void setItemDelegate ( QAbstractItemDelegate * delegate )
- void setModel ( QAbstractItemModel * model )
- void setOrientation ( Qt::Orientation orientation )
- void setRootIndex ( const QModelIndex & index )
- void setSubmitPolicy ( SubmitPolicy policy )
- SubmitPolicy submitPolicy () const

- 28 public functions inherited from QObject

### Public Slots

- void revert ()
- virtual void setCurrentIndex ( int index )
- void setCurrentModelIndex ( const QModelIndex & index )
- bool submit ()
- void toFirst ()
- void toLast ()
- void toNext ()
- void toPrevious ()

- 1 public slot inherited from QObject

### Signals

- void currentIndexChanged ( int index )

- 1 signal inherited from QObject

### Additional Inherited Members

- 5 static public members inherited from QObject
- 7 protected functions inherited from QObject

## 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, all widgets are updated with the contents from the model. If user edits the contents of the widget, the changes are written back to the model.

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 | Trolltech ASA | Oslo
2 | Trolltech Pty | Brisbane
3 | Trolltech Inc | Palo Alto
4 | Trolltech China | Beijing
5 | Trolltech GmbH | 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->first();

After the call to first(), `mySpinBox` displays the value `1`, `myLineEdit` displays `Trolltech ASA` 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.

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

See also QAbstractItemModel and QAbstractItemDelegate.

## Member Type Documentation

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

## Property Documentation

### currentIndex : int

Access functions:

- int currentIndex () const
- virtual void setCurrentIndex ( int index )

### orientation : Qt::Orientation

Access functions:

- Qt::Orientation orientation () const
- void setOrientation ( Qt::Orientation orientation )

### submitPolicy : SubmitPolicy

Access functions:

- SubmitPolicy submitPolicy () const
- void setSubmitPolicy ( SubmitPolicy policy )

## Member Function Documentation

### QDataWidgetMapper::QDataWidgetMapper ( QObject * parent = 0 )

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::~QDataWidgetMapper ()

Destroys the object.

### void QDataWidgetMapper::addMapping ( 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 containing the name of a person, the second column his age. The first column is mapped to the QLineEdit `nameLineEdit` and the second to the QSpinBox `ageSpinBox`:

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

Note: If the widget is already mapped to a section, the old mapping will be replaced by the new one. A widget can never be mapped to more than one section at a time.

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

### void QDataWidgetMapper::clearMapping ()

Clears all mappings.

See also addMapping() and removeMapping().

### void QDataWidgetMapper::currentIndexChanged ( int index ) `[signal]`

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

See also currentIndex() and setCurrentIndex().

### QAbstractItemDelegate * QDataWidgetMapper::itemDelegate () const

Returns the current item delegate.

See also setItemDelegate() and setDelegate().

### int QDataWidgetMapper::mappedSection ( QWidget * widget ) const

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

See also addMapping() and removeMapping().

### QWidget * QDataWidgetMapper::mappedWidgetAt ( int section ) const

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 () const

Returns the current model.

See also setModel().

### void QDataWidgetMapper::removeMapping ( QWidget * widget )

Removes the mapping for the given widget.

See also addMapping() and clearMapping().

### void QDataWidgetMapper::revert () `[slot]`

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

See also submit() and setSubmitPolicy().

### QModelIndex QDataWidgetMapper::rootIndex () const

Returns the current root index.

See also setRootIndex().

### void QDataWidgetMapper::setCurrentModelIndex ( const QModelIndex & index ) `[slot]`

### void QDataWidgetMapper::setItemDelegate ( 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().

See also QAbstractItemDelegate and itemDelegate().

### void QDataWidgetMapper::setModel ( QAbstractItemModel * model )

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

See also model().

### void QDataWidgetMapper::setRootIndex ( const 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().

### bool QDataWidgetMapper::submit () `[slot]`

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 setSumbitPolicy().

### void QDataWidgetMapper::toFirst () `[slot]`

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

### void QDataWidgetMapper::toLast () `[slot]`

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 toLast() and setCurrentIndex().

### void QDataWidgetMapper::toNext () `[slot]`

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

### void QDataWidgetMapper::toPrevious () `[slot]`

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