CopperSpice API  1.7.2
QDataWidgetMapper Class Reference

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

Inheritance diagram for QDataWidgetMapper:
QObject

Public Types

enum  SubmitPolicy
 

Public Signals

void currentIndexChanged (int index)
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Slots

void revert ()
 
virtual void setCurrentIndex (int index)
 
void setCurrentModelIndex (const QModelIndex &index)
 
bool submit ()
 
void toFirst ()
 
void toLast ()
 
void toNext ()
 
void toPrevious ()
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QDataWidgetMapper (QObject *parent=nullptr)
 
 ~QDataWidgetMapper ()
 
void addMapping (QWidget *widget, int section)
 
void addMapping (QWidget *widget, int section, const QString &propertyName)
 
void clearMapping ()
 
int currentIndex () const
 
QAbstractItemDelegateitemDelegate () const
 
QString mappedPropertyName (QWidget *widget) const
 
int mappedSection (QWidget *widget) const
 
QWidgetmappedWidgetAt (int section) const
 
QAbstractItemModelmodel () const
 
Qt::Orientation orientation () const
 
void removeMapping (QWidget *widget)
 
QModelIndex rootIndex () const
 
void setItemDelegate (QAbstractItemDelegate *delegate)
 
void setModel (QAbstractItemModel *model)
 
void setOrientation (Qt::Orientation aOrientation)
 
void setRootIndex (const QModelIndex &index)
 
void setSubmitPolicy (SubmitPolicy policy)
 
SubmitPolicy submitPolicy () const
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &objName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Properties

 currentIndex
 
 orientation
 
 submitPolicy
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

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

1CanadaMontreal
2AustraliaDarwin
3USASan Francisco
4ChinaBeijing
5FranceParis

The following code will map the columns of the model to widgets called mySpinBox, myLineEdit and myCountryChooser:

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 Canada and myCountryChooser displays Montreal. 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 will not 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, QAbstractItemDelegate

Member Enumeration Documentation

This enum describes the possible submit policies a QDataWidgetMapper supports.

ConstantValueDescription
QDataWidgetMapper::AutoSubmit0Whenever a widget loses focus, the widget's current value is set to the item model.
QDataWidgetMapper::ManualSubmit1The model is not updated until submit() is called.

Constructor & Destructor Documentation

QDataWidgetMapper::QDataWidgetMapper ( QObject parent = nullptr)
explicit

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

See also
setOrientation(), setSubmitPolicy()
QDataWidgetMapper::~QDataWidgetMapper ( )

Destroys the object.

Method Documentation

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 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->setModel(myModel);
mapper->addMapping(nameLineEdit, 0);
mapper->addMapping(ageSpinBox, 1);
  • 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.
See also
removeMapping(), mappedSection(), clearMapping()
void QDataWidgetMapper::addMapping ( QWidget widget,
int  section,
const QString propertyName 
)

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

See also
addMapping()
void QDataWidgetMapper::clearMapping ( )

Clears all mappings.

See also
addMapping(), removeMapping()
int QDataWidgetMapper::currentIndex ( ) const

Property documentation pending.

void QDataWidgetMapper::currentIndexChanged ( int  index)
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(), setCurrentIndex()
QAbstractItemDelegate * QDataWidgetMapper::itemDelegate ( ) const

Returns the current item delegate.

See also
setItemDelegate()
QString QDataWidgetMapper::mappedPropertyName ( QWidget widget) const

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

See also
mappedSection(), addMapping(), removeMapping()
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(), 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(), removeMapping()
QAbstractItemModel * QDataWidgetMapper::model ( ) const

Returns the current model.

See also
setModel()
Qt::Orientation QDataWidgetMapper::orientation ( ) const

Property documentation pending.

void QDataWidgetMapper::removeMapping ( QWidget widget)

Removes the mapping for the given widget.

See also
addMapping(), clearMapping()
void QDataWidgetMapper::revert ( )
slot

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

See also
submit(), setSubmitPolicy()
QModelIndex QDataWidgetMapper::rootIndex ( ) const

Returns the current root index.

See also
setRootIndex()
void QDataWidgetMapper::setCurrentIndex ( int  index)
slot

Property documentation pending.

void QDataWidgetMapper::setCurrentModelIndex ( const QModelIndex index)
slot

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:

connect(myTableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
See also
currentIndex()
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().

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()
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::setOrientation ( Qt::Orientation  aOrientation)

Property documentation pending.

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()
void QDataWidgetMapper::setSubmitPolicy ( SubmitPolicy  policy)

Property documentation pending.

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(), setSubmitPolicy()
SubmitPolicy QDataWidgetMapper::submitPolicy ( ) const

Property documentation pending.

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(), 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
toFirst(), 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(), 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(), setCurrentIndex()

Property Documentation

QDataWidgetMapper::currentIndex

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.

See also
setCurrentModelIndex(), toFirst(), toNext(), toPrevious(), toLast()
PropertiesClass Methods
read currentIndex
write setCurrentIndex
notify currentIndexChanged
QDataWidgetMapper::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:

1CanadaMontreal
2AustraliaDarwin
3USASan Francisco
4ChinaBeijing
5FranceParis

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
Canada Australia USA China France
Montreal Darwin San Francisco Beijing Paris

Changing the orientation clears all existing mappings.

PropertiesClass Methods
read orientation
write setOrientation
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.

PropertiesClass Methods
read submitPolicy
write setSubmitPolicy