QDataWidgetMapper Class
The QDataWidgetMapper class provides mapping between a section of a data model to widgets. More...
| Header: | #include <QDataWidgetMapper> |
| qmake: | QT += widgets |
| Since: | Qt 4.2 |
| Inherits: | QObject |
This class was introduced in Qt 4.2.
Public Types
| enum | SubmitPolicy { AutoSubmit, ManualSubmit } |
Properties
- currentIndex : int
- orientation : Qt::Orientation
- submitPolicy : SubmitPolicy
Public Functions
| int | currentIndex() const |
| Qt::Orientation | orientation() const |
| void | setOrientation(Qt::Orientation aOrientation) |
| void | setSubmitPolicy(QDataWidgetMapper::SubmitPolicy policy) |
| QDataWidgetMapper::SubmitPolicy | submitPolicy() const |
Public Slots
| virtual void | setCurrentIndex(int index) |
Signals
| void | currentIndexChanged(int index) |
Detailed Description
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.
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
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.
Access functions:
| int | currentIndex() const |
| virtual void | setCurrentIndex(int index) |
Notifier signal:
| void | currentIndexChanged(int index) |
See also setCurrentModelIndex(), toFirst(), toNext(), toPrevious(), and toLast().
orientation : 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 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 setCurrentIndex() will change the current column. The widget will be populates with the model's data from its mapped row and the column that 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.
Access functions:
| Qt::Orientation | orientation() const |
| void | setOrientation(Qt::Orientation aOrientation) |
submitPolicy : SubmitPolicy
This property holds the current submit policy
Changing the current submit policy will revert all widgets to the current data from the model.
Access functions:
| QDataWidgetMapper::SubmitPolicy | submitPolicy() const |
| void | setSubmitPolicy(QDataWidgetMapper::SubmitPolicy policy) |