Qt 4.8
|
The QDataWidgetMapper class provides mapping between a section of a data model to widgets. More...
#include <qdatawidgetmapper.h>
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 |
QAbstractItemDelegate * | itemDelegate () 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... | |
QWidget * | mappedWidgetAt (int section) const |
Returns the widget that is mapped at section, or 0 if no widget is mapped at that section. More... | |
QAbstractItemModel * | model () 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 QObjectList & | children () 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< QByteArray > | dynamicPropertyNames () 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 > | |
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 QMetaObject * | metaObject () 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 |
QObject * | parent () 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... | |
QThread * | thread () const |
Returns the thread in which the object lives. More... | |
QObjectUserData * | userData (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... | |
QObject * | sender () 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< QObjectData > | d_ptr |
Static Protected Variables inherited from QObject | |
static const QMetaObject | staticQtMetaObject |
Related Functions inherited from QObject | |
T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QRegExp ®Exp)() |
T * | qobject_cast (QObject *object) |
QObjectList | |
void * | qt_find_obj_child (QObject *parent, const char *type, const QString &name) |
Returns a pointer to the object named name that inherits type and with a given parent. More... | |
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; 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
:
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.
Definition at line 60 of file qdatawidgetmapper.h.
This enum describes the possible submit policies a QDataWidgetMapper supports.
Enumerator | |
---|---|
AutoSubmit | |
ManualSubmit |
Definition at line 85 of file qdatawidgetmapper.h.
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
.
Definition at line 351 of file qdatawidgetmapper.cpp.
QDataWidgetMapper::~QDataWidgetMapper | ( | ) |
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
:
Notes:
Definition at line 498 of file qdatawidgetmapper.cpp.
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.
Definition at line 519 of file qdatawidgetmapper.cpp.
void QDataWidgetMapper::clearMapping | ( | ) |
Clears all mappings.
Definition at line 774 of file qdatawidgetmapper.cpp.
Referenced by setModel(), and setOrientation().
int QDataWidgetMapper::currentIndex | ( | ) | const |
Referenced by setCurrentIndex().
|
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.
Referenced by setCurrentIndex().
QAbstractItemDelegate * QDataWidgetMapper::itemDelegate | ( | ) | const |
Returns the current item delegate.
Definition at line 444 of file qdatawidgetmapper.cpp.
QByteArray QDataWidgetMapper::mappedPropertyName | ( | QWidget * | widget | ) | const |
Returns the name of the property that is used when mapping data to the given widget.
Definition at line 573 of file qdatawidgetmapper.cpp.
int QDataWidgetMapper::mappedSection | ( | QWidget * | widget | ) | const |
Returns the section the widget is mapped to or -1 if the widget is not mapped.
Definition at line 551 of file qdatawidgetmapper.cpp.
QWidget * QDataWidgetMapper::mappedWidgetAt | ( | int | section | ) | const |
Returns the widget that is mapped at section, or 0 if no widget is mapped at that section.
Definition at line 593 of file qdatawidgetmapper.cpp.
QAbstractItemModel * QDataWidgetMapper::model | ( | ) | const |
Returns the current model.
Definition at line 399 of file qdatawidgetmapper.cpp.
Referenced by setModel().
Qt::Orientation QDataWidgetMapper::orientation | ( | ) | const |
Referenced by setOrientation().
void QDataWidgetMapper::removeMapping | ( | QWidget * | widget | ) |
Removes the mapping for the given widget.
Definition at line 533 of file qdatawidgetmapper.cpp.
Referenced by addMapping().
|
slot |
Repopulates all widgets with the current data of the model.
All unsubmitted changes will be lost.
Definition at line 611 of file qdatawidgetmapper.cpp.
Referenced by setSubmitPolicy().
QModelIndex QDataWidgetMapper::rootIndex | ( | ) | const |
Returns the current root index.
Definition at line 468 of file qdatawidgetmapper.cpp.
|
virtualslot |
Definition at line 720 of file qdatawidgetmapper.cpp.
Referenced by setCurrentModelIndex(), toFirst(), toLast(), toNext(), and toPrevious().
|
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:
Definition at line 757 of file qdatawidgetmapper.cpp.
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().
Definition at line 420 of file qdatawidgetmapper.cpp.
Referenced by QDataWidgetMapper().
void QDataWidgetMapper::setModel | ( | QAbstractItemModel * | model | ) |
Sets the current model to model.
If another model was set, all mappings to that old model are cleared.
Definition at line 370 of file qdatawidgetmapper.cpp.
void QDataWidgetMapper::setOrientation | ( | Qt::Orientation | aOrientation | ) |
Definition at line 822 of file qdatawidgetmapper.cpp.
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.
Definition at line 457 of file qdatawidgetmapper.cpp.
void QDataWidgetMapper::setSubmitPolicy | ( | SubmitPolicy | policy | ) |
Definition at line 849 of file qdatawidgetmapper.cpp.
|
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.
Definition at line 632 of file qdatawidgetmapper.cpp.
SubmitPolicy QDataWidgetMapper::submitPolicy | ( | ) | const |
Referenced by setSubmitPolicy().
|
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)
.
Definition at line 654 of file qdatawidgetmapper.cpp.
|
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.
Definition at line 668 of file qdatawidgetmapper.cpp.
|
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.
Definition at line 685 of file qdatawidgetmapper.cpp.
|
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.
Definition at line 701 of file qdatawidgetmapper.cpp.
|
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.
Definition at line 65 of file qdatawidgetmapper.h.
|
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.
|
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.