Qt 4.8
Public Types | Public Slots | Signals | Public Functions | Properties | List of all members
QDataWidgetMapper Class Reference

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

#include <qdatawidgetmapper.h>

Inheritance diagram for QDataWidgetMapper:
QObject

Public Types

enum  SubmitPolicy { AutoSubmit, ManualSubmit }
 This enum describes the possible submit policies a QDataWidgetMapper supports. More...
 

Public Slots

void revert ()
 Repopulates all widgets with the current data of the model. More...
 
virtual void setCurrentIndex (int index)
 
void setCurrentModelIndex (const QModelIndex &index)
 Sets the current index to the row of the index if the orientation is horizontal (the default), otherwise to the column of the index. More...
 
bool submit ()
 Submits all changes from the mapped widgets to the model. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 

Signals

void currentIndexChanged (int index)
 This signal is emitted after the current index has changed and all widgets were populated with new data. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 

Public Functions

void addMapping (QWidget *widget, int section)
 Adds a mapping between a widget and a section from the model. More...
 
void addMapping (QWidget *widget, int section, const QByteArray &propertyName)
 Essentially the same as addMapping(), but adds the possibility to specify the property to use specifying propertyName. More...
 
void clearMapping ()
 Clears all mappings. More...
 
int currentIndex () const
 
QAbstractItemDelegateitemDelegate () const
 Returns the current item delegate. More...
 
QByteArray mappedPropertyName (QWidget *widget) const
 Returns the name of the property that is used when mapping data to the given widget. More...
 
int mappedSection (QWidget *widget) const
 Returns the section the widget is mapped to or -1 if the widget is not mapped. More...
 
QWidgetmappedWidgetAt (int section) const
 Returns the widget that is mapped at section, or 0 if no widget is mapped at that section. More...
 
QAbstractItemModelmodel () const
 Returns the current model. More...
 
Qt::Orientation orientation () const
 
 QDataWidgetMapper (QObject *parent=0)
 Constructs a new QDataWidgetMapper with parent object parent. More...
 
void removeMapping (QWidget *widget)
 Removes the mapping for the given widget. More...
 
QModelIndex rootIndex () const
 Returns the current root index. More...
 
void setItemDelegate (QAbstractItemDelegate *delegate)
 Sets the item delegate to delegate. More...
 
void setModel (QAbstractItemModel *model)
 Sets the current model to model. More...
 
void setOrientation (Qt::Orientation aOrientation)
 
void setRootIndex (const QModelIndex &index)
 Sets the root item to index. More...
 
void setSubmitPolicy (SubmitPolicy policy)
 
SubmitPolicy submitPolicy () const
 
 ~QDataWidgetMapper ()
 Destroys the object. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Properties

int currentIndex
 the current row or column More...
 
Qt::Orientation orientation
 the orientation of the model More...
 
SubmitPolicy submitPolicy
 the current submit policy More...
 

Additional Inherited Members

- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

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

Since
4.2

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

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, QAbstractItemDelegate

Definition at line 60 of file qdatawidgetmapper.h.

Enumerations

◆ SubmitPolicy

This enum describes the possible submit policies a QDataWidgetMapper supports.

  • AutoSubmit Whenever a widget loses focus, the widget's current value is set to the item model.
  • ManualSubmit The model is not updated until submit() is called.
Enumerator
AutoSubmit 
ManualSubmit 

Definition at line 85 of file qdatawidgetmapper.h.

Constructors and Destructors

◆ QDataWidgetMapper()

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(), setSubmitPolicy()

Definition at line 351 of file qdatawidgetmapper.cpp.

352  : QObject(*new QDataWidgetMapperPrivate, parent)
353 {
354  setItemDelegate(new QItemDelegate(this));
355 }
void setItemDelegate(QAbstractItemDelegate *delegate)
Sets the item delegate to delegate.
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753

◆ ~QDataWidgetMapper()

QDataWidgetMapper::~QDataWidgetMapper ( )

Destroys the object.

Definition at line 360 of file qdatawidgetmapper.cpp.

361 {
362 }

Functions

◆ addMapping() [1/2]

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);

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.
See also
removeMapping(), mappedSection(), clearMapping()

Definition at line 498 of file qdatawidgetmapper.cpp.

499 {
501 
502  removeMapping(widget);
503  d->widgetMap.append(QDataWidgetMapperPrivate::WidgetMapper(widget, section, d->indexAt(section)));
504  widget->installEventFilter(d->delegate);
505 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
void removeMapping(QWidget *widget)
Removes the mapping for the given widget.
void installEventFilter(QObject *)
Installs an event filter filterObj on this object.
Definition: qobject.cpp:2070
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ addMapping() [2/2]

void QDataWidgetMapper::addMapping ( QWidget widget,
int  section,
const QByteArray propertyName 
)

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

Since
4.3
See also
addMapping()

Definition at line 519 of file qdatawidgetmapper.cpp.

520 {
522 
523  removeMapping(widget);
524  d->widgetMap.append(QDataWidgetMapperPrivate::WidgetMapper(widget, section, d->indexAt(section), propertyName));
525  widget->installEventFilter(d->delegate);
526 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
void removeMapping(QWidget *widget)
Removes the mapping for the given widget.
void installEventFilter(QObject *)
Installs an event filter filterObj on this object.
Definition: qobject.cpp:2070
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ clearMapping()

void QDataWidgetMapper::clearMapping ( )

Clears all mappings.

See also
addMapping(), removeMapping()

Definition at line 774 of file qdatawidgetmapper.cpp.

Referenced by setModel(), and setOrientation().

775 {
777 
778  while (!d->widgetMap.isEmpty()) {
779  QWidget *w = d->widgetMap.takeLast().widget;
780  if (w)
781  w->removeEventFilter(d->delegate);
782  }
783 }
double d
Definition: qnumeric_p.h:62
void removeEventFilter(QObject *)
Removes an event filter object obj from this object.
Definition: qobject.cpp:2099
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
#define Q_D(Class)
Definition: qglobal.h:2482
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ currentIndex()

int QDataWidgetMapper::currentIndex ( ) const

Referenced by setCurrentIndex().

◆ currentIndexChanged

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

Referenced by setCurrentIndex().

◆ itemDelegate()

QAbstractItemDelegate * QDataWidgetMapper::itemDelegate ( ) const

Returns the current item delegate.

Definition at line 444 of file qdatawidgetmapper.cpp.

445 {
446  Q_D(const QDataWidgetMapper);
447  return d->delegate;
448 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ mappedPropertyName()

QByteArray QDataWidgetMapper::mappedPropertyName ( QWidget widget) const

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

Since
4.3
See also
mappedSection(), addMapping(), removeMapping()

Definition at line 573 of file qdatawidgetmapper.cpp.

574 {
575  Q_D(const QDataWidgetMapper);
576 
577  int idx = d->findWidget(widget);
578  if (idx == -1)
579  return QByteArray();
580  const QDataWidgetMapperPrivate::WidgetMapper &m = d->widgetMap.at(idx);
581  if (m.property.isEmpty())
582  return m.widget->metaObject()->userProperty().name();
583  else
584  return m.property;
585 }
double d
Definition: qnumeric_p.h:62
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define Q_D(Class)
Definition: qglobal.h:2482
const char * name() const
Returns this property&#39;s name.
QMetaProperty userProperty() const
Returns the property that has the USER flag set to true.
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.

◆ mappedSection()

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

Definition at line 551 of file qdatawidgetmapper.cpp.

552 {
553  Q_D(const QDataWidgetMapper);
554 
555  int idx = d->findWidget(widget);
556  if (idx == -1)
557  return -1;
558 
559  return d->widgetMap.at(idx).section;
560 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ mappedWidgetAt()

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

Definition at line 593 of file qdatawidgetmapper.cpp.

594 {
595  Q_D(const QDataWidgetMapper);
596 
597  for (int i = 0; i < d->widgetMap.count(); ++i) {
598  if (d->widgetMap.at(i).section == section)
599  return d->widgetMap.at(i).widget;
600  }
601 
602  return 0;
603 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ model()

QAbstractItemModel * QDataWidgetMapper::model ( ) const

Returns the current model.

See also
setModel()

Definition at line 399 of file qdatawidgetmapper.cpp.

Referenced by setModel().

400 {
401  Q_D(const QDataWidgetMapper);
403  ? static_cast<QAbstractItemModel *>(0)
404  : d->model;
405 }
double d
Definition: qnumeric_p.h:62
static QAbstractItemModel * staticEmptyModel()
#define Q_D(Class)
Definition: qglobal.h:2482
The QAbstractItemModel class provides the abstract interface for item model classes.
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ orientation()

Qt::Orientation QDataWidgetMapper::orientation ( ) const

Referenced by setOrientation().

◆ removeMapping()

void QDataWidgetMapper::removeMapping ( QWidget widget)

Removes the mapping for the given widget.

See also
addMapping(), clearMapping()

Definition at line 533 of file qdatawidgetmapper.cpp.

Referenced by addMapping().

534 {
536 
537  int idx = d->findWidget(widget);
538  if (idx == -1)
539  return;
540 
541  d->widgetMap.removeAt(idx);
542  widget->removeEventFilter(d->delegate);
543 }
double d
Definition: qnumeric_p.h:62
void removeEventFilter(QObject *)
Removes an event filter object obj from this object.
Definition: qobject.cpp:2099
#define Q_D(Class)
Definition: qglobal.h:2482
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ revert

void QDataWidgetMapper::revert ( )
slot

Repopulates all widgets with the current data of the model.

All unsubmitted changes will be lost.

See also
submit(), setSubmitPolicy()

Definition at line 611 of file qdatawidgetmapper.cpp.

Referenced by setSubmitPolicy().

612 {
614 
615  d->populate();
616 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ rootIndex()

QModelIndex QDataWidgetMapper::rootIndex ( ) const

Returns the current root index.

See also
setRootIndex()

Definition at line 468 of file qdatawidgetmapper.cpp.

469 {
470  Q_D(const QDataWidgetMapper);
471  return QModelIndex(d->rootIndex);
472 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QModelIndex class is used to locate data in a data model.
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ setCurrentIndex

void QDataWidgetMapper::setCurrentIndex ( int  index)
virtualslot

Definition at line 720 of file qdatawidgetmapper.cpp.

Referenced by setCurrentModelIndex(), toFirst(), toLast(), toNext(), and toPrevious().

721 {
723 
724  if (index < 0 || index >= d->itemCount())
725  return;
726  d->currentTopLeft = d->orientation == Qt::Horizontal
727  ? d->model->index(index, 0, d->rootIndex)
728  : d->model->index(0, index, d->rootIndex);
729  d->populate();
730 
732 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
#define emit
Definition: qobjectdefs.h:76
quint16 index
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...
void currentIndexChanged(int index)
This signal is emitted after the current index has changed and all widgets were populated with new da...

◆ setCurrentModelIndex

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 QItemSelectionModel::currentRowChanged() or QItemSelectionModel::currentColumnChanged() of another view's QItemSelectionModel{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()

Definition at line 757 of file qdatawidgetmapper.cpp.

758 {
760 
761  if (!index.isValid()
762  || index.model() != d->model
763  || index.parent() != d->rootIndex)
764  return;
765 
766  setCurrentIndex(d->orientation == Qt::Horizontal ? index.row() : index.column());
767 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
virtual void setCurrentIndex(int index)
int row() const
Returns the row this model index refers to.
const QAbstractItemModel * model() const
Returns a pointer to the model containing the item that this index refers to.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...
int column() const
Returns the column this model index refers to.

◆ setItemDelegate()

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

Definition at line 420 of file qdatawidgetmapper.cpp.

Referenced by QDataWidgetMapper().

421 {
423  QAbstractItemDelegate *oldDelegate = d->delegate;
424  if (oldDelegate) {
425  disconnect(oldDelegate, SIGNAL(commitData(QWidget*)), this, SLOT(_q_commitData(QWidget*)));
426  disconnect(oldDelegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
427  this, SLOT(_q_closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
428  }
429 
430  d->delegate = delegate;
431 
432  if (delegate) {
433  connect(delegate, SIGNAL(commitData(QWidget*)), SLOT(_q_commitData(QWidget*)));
434  connect(delegate, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)),
436  }
437 
438  d->flipEventFilters(oldDelegate, delegate);
439 }
double d
Definition: qnumeric_p.h:62
The QAbstractItemDelegate class is used to display and edit data items from a model.
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
#define Q_D(Class)
Definition: qglobal.h:2482
#define SIGNAL(a)
Definition: qobjectdefs.h:227
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
EndEditHint
This enum describes the different hints that the delegate can give to the model and view components t...
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ setModel()

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

Definition at line 370 of file qdatawidgetmapper.cpp.

371 {
373 
374  if (d->model == model)
375  return;
376 
377  if (d->model) {
378  disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this,
379  SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
380  disconnect(d->model, SIGNAL(destroyed()), this,
381  SLOT(_q_modelDestroyed()));
382  }
383  clearMapping();
384  d->rootIndex = QModelIndex();
385  d->currentTopLeft = QModelIndex();
386 
387  d->model = model;
388 
389  connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
390  SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
391  connect(model, SIGNAL(destroyed()), SLOT(_q_modelDestroyed()));
392 }
double d
Definition: qnumeric_p.h:62
#define SLOT(a)
Definition: qobjectdefs.h:226
#define Q_D(Class)
Definition: qglobal.h:2482
QAbstractItemModel * model() const
Returns the current model.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
void clearMapping()
Clears all mappings.
The QModelIndex class is used to locate data in a data model.
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ setOrientation()

void QDataWidgetMapper::setOrientation ( Qt::Orientation  aOrientation)

Definition at line 822 of file qdatawidgetmapper.cpp.

823 {
825 
826  if (d->orientation == orientation)
827  return;
828 
829  clearMapping();
830  d->orientation = orientation;
831 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
Qt::Orientation orientation() const
void clearMapping()
Clears all mappings.
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ setRootIndex()

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

Definition at line 457 of file qdatawidgetmapper.cpp.

458 {
460  d->rootIndex = index;
461 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
quint16 index
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ setSubmitPolicy()

void QDataWidgetMapper::setSubmitPolicy ( SubmitPolicy  policy)

Definition at line 849 of file qdatawidgetmapper.cpp.

850 {
852  if (policy == d->submitPolicy)
853  return;
854 
855  revert();
856  d->submitPolicy = policy;
857 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
void revert()
Repopulates all widgets with the current data of the model.
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ submit

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

See also
revert(), setSubmitPolicy()

Definition at line 632 of file qdatawidgetmapper.cpp.

633 {
635 
636  for (int i = 0; i < d->widgetMap.count(); ++i) {
637  const QDataWidgetMapperPrivate::WidgetMapper &m = d->widgetMap.at(i);
638  if (!d->commit(m))
639  return false;
640  }
641 
642  return d->model->submit();
643 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ submitPolicy()

SubmitPolicy QDataWidgetMapper::submitPolicy ( ) const

Referenced by setSubmitPolicy().

◆ toFirst

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

Definition at line 654 of file qdatawidgetmapper.cpp.

655 {
656  setCurrentIndex(0);
657 }
virtual void setCurrentIndex(int index)

◆ toLast

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

Definition at line 668 of file qdatawidgetmapper.cpp.

669 {
671  setCurrentIndex(d->itemCount() - 1);
672 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void setCurrentIndex(int index)
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ toNext

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

Definition at line 685 of file qdatawidgetmapper.cpp.

686 {
688  setCurrentIndex(d->currentIdx() + 1);
689 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void setCurrentIndex(int index)
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

◆ toPrevious

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

Definition at line 701 of file qdatawidgetmapper.cpp.

702 {
704  setCurrentIndex(d->currentIdx() - 1);
705 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void setCurrentIndex(int index)
The QDataWidgetMapper class provides mapping between a section of a data model to widgets...

Properties

◆ currentIndex

int QDataWidgetMapper::currentIndex
private

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

Definition at line 65 of file qdatawidgetmapper.h.

◆ orientation

Qt::Orientation QDataWidgetMapper::orientation
private

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.

Definition at line 66 of file qdatawidgetmapper.h.

◆ submitPolicy

QDataWidgetMapper::SubmitPolicy QDataWidgetMapper::submitPolicy
private

the current submit policy

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

Definition at line 67 of file qdatawidgetmapper.h.


The documentation for this class was generated from the following files: