QtBase
v6.3.1
|
The QDataWidgetMapper class provides mapping between a section of a data model to widgets. More...
#include <qdatawidgetmapper.h>
Public Types | |
enum | SubmitPolicy { AutoSubmit , ManualSubmit } |
Public Slots | |
void | revert () |
bool | submit () |
void | toFirst () |
void | toLast () |
void | toNext () |
void | toPrevious () |
virtual void | setCurrentIndex (int index) |
void | setCurrentModelIndex (const QModelIndex &index) |
Public Slots inherited from QObject | |
void | deleteLater () |
Signals | |
void | currentIndexChanged (int index) |
Signals inherited from QObject | |
void | destroyed (QObject *=nullptr) |
void | objectNameChanged (const QString &objectName, QPrivateSignal) |
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... | |
Properties inherited from QObject | |
QString | objectName |
the name of this object More... | |
The QDataWidgetMapper class provides mapping between a section of a data model to widgets.
\inmodule QtWidgets
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 \l{Q_PROPERTY()}{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:
\table \row
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 55 of file qdatawidgetmapper.h.
This enum describes the possible submit policies a QDataWidgetMapper supports.
\value AutoSubmit Whenever a widget loses focus, the widget's current value is set to the item model. \value ManualSubmit The model is not updated until submit() is called.
Enumerator | |
---|---|
AutoSubmit | |
ManualSubmit |
Definition at line 79 of file qdatawidgetmapper.h.
Constructs a new QDataWidgetMapper with parent object parent. By default, the orientation is horizontal and the submit policy is {AutoSubmit}.
Definition at line 327 of file qdatawidgetmapper.cpp.
QDataWidgetMapper::~QDataWidgetMapper | ( | ) |
Destroys the object.
Definition at line 336 of file qdatawidgetmapper.cpp.
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:} \list
Definition at line 477 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 495 of file qdatawidgetmapper.cpp.
void QDataWidgetMapper::clearMapping | ( | ) |
Clears all mappings.
Definition at line 745 of file qdatawidgetmapper.cpp.
int QDataWidgetMapper::currentIndex | ( | ) | const |
Definition at line 703 of file qdatawidgetmapper.cpp.
|
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.
QAbstractItemDelegate * QDataWidgetMapper::itemDelegate | ( | ) | const |
Returns the current item delegate.
Definition at line 423 of file qdatawidgetmapper.cpp.
QByteArray QDataWidgetMapper::mappedPropertyName | ( | QWidget * | widget | ) | const |
Definition at line 546 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 527 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 566 of file qdatawidgetmapper.cpp.
QAbstractItemModel * QDataWidgetMapper::model | ( | ) | const |
Returns the current model.
Definition at line 375 of file qdatawidgetmapper.cpp.
Qt::Orientation QDataWidgetMapper::orientation | ( | ) | const |
Definition at line 802 of file qdatawidgetmapper.cpp.
Removes the mapping for the given widget.
Definition at line 509 of file qdatawidgetmapper.cpp.
|
slot |
Repopulates all widgets with the current data of the model. All unsubmitted changes will be lost.
Definition at line 584 of file qdatawidgetmapper.cpp.
QModelIndex QDataWidgetMapper::rootIndex | ( | ) | const |
Returns the current root index.
Definition at line 447 of file qdatawidgetmapper.cpp.
|
virtualslot |
|
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 \l {QItemSelectionModel::}{currentRowChanged()} or \l {QItemSelectionModel::}{currentColumnChanged()} of another view's \l {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 728 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().
Any existing delegate will be removed, but not deleted. QDataWidgetMapper does not take ownership of delegate.
The delegate also decides when to apply data and when to change the editor, using QAbstractItemDelegate::commitData() and QAbstractItemDelegate::closeEditor().
Definition at line 399 of file qdatawidgetmapper.cpp.
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 346 of file qdatawidgetmapper.cpp.
void QDataWidgetMapper::setOrientation | ( | Qt::Orientation | aOrientation | ) |
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 436 of file qdatawidgetmapper.cpp.
void QDataWidgetMapper::setSubmitPolicy | ( | SubmitPolicy | policy | ) |
Definition at line 815 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 \l {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 605 of file qdatawidgetmapper.cpp.
QDataWidgetMapper::SubmitPolicy QDataWidgetMapper::submitPolicy | ( | ) | const |
Definition at line 825 of file qdatawidgetmapper.cpp.
|
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 626 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 640 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 657 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 673 of file qdatawidgetmapper.cpp.
|
readwrite |
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 119 of file qdatawidgetmapper.h.
|
readwrite |
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:
\table \row
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:
\table \row
Changing the orientation clears all existing mappings.
Definition at line 119 of file qdatawidgetmapper.h.
|
readwrite |
the current submit policy
Changing the current submit policy will revert all widgets to the current data from the model.
Definition at line 119 of file qdatawidgetmapper.h.