|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QObject
com.trolltech.qt.gui.QDataWidgetMapper
public class QDataWidgetMapper
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. 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 | Nokia Corporation and/or its subsidiary(-ies) | Oslo |
2 | Trolltech Pty | Brisbane |
3 | Trolltech Inc | Palo Alto |
4 | Trolltech China | Beijing |
5 | Trolltech GmbH | Berlin |
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 Nokia Corporation and/or its subsidiary(-ies) 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.
QAbstractItemModel
, and QAbstractItemDelegate
.
Nested Class Summary | |
---|---|
static class |
QDataWidgetMapper.SubmitPolicy
This enum describes the possible submit policies a QDataWidgetMapper supports. |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary | |
---|---|
QSignalEmitter.Signal1 |
currentIndexChanged
This signal takes 1 generic argument(s). |
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
QDataWidgetMapper()
Constructs a new QDataWidgetMapper with parent object parent. |
|
QDataWidgetMapper(QObject parent)
Constructs a new QDataWidgetMapper with parent object parent. |
Method Summary | |
---|---|
void |
addMapping(QWidget widget,
int section)
Adds a mapping between a widget and a section from the model. |
void |
addMapping(QWidget widget,
int section,
QByteArray propertyName)
Essentially the same as addMapping() , but adds the possibility to specify the property to use specifying propertyName. |
void |
clearMapping()
Clears all mappings. |
int |
currentIndex()
This property holds the current row or column. |
QAbstractItemDelegate |
itemDelegate()
Returns the current item delegate. |
QByteArray |
mappedPropertyName(QWidget widget)
Returns the name of the property that is used when mapping data to the given widget. |
int |
mappedSection(QWidget widget)
Returns the section the widget is mapped to or -1 if the widget is not mapped. |
QWidget |
mappedWidgetAt(int section)
Returns the widget that is mapped at section, or 0 if no widget is mapped at that section. |
QAbstractItemModel |
model()
Returns the current model. |
Qt.Orientation |
orientation()
This property holds the orientation of the model. |
void |
removeMapping(QWidget widget)
Removes the mapping for the given widget. |
void |
revert()
Repopulates all widgets with the current data of the model. |
com.trolltech.qt.core.QModelIndex |
rootIndex()
Returns the current root index. |
void |
setCurrentIndex(int index)
This property holds the current row or column. |
void |
setCurrentModelIndex(com.trolltech.qt.core.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. |
void |
setItemDelegate(QAbstractItemDelegate delegate)
Sets the item delegate to delegate. |
void |
setModel(QAbstractItemModel model)
Sets the current model to model. |
void |
setOrientation(Qt.Orientation aOrientation)
This property holds the orientation of the model. |
void |
setRootIndex(com.trolltech.qt.core.QModelIndex index)
Sets the root item to index. |
void |
setSubmitPolicy(QDataWidgetMapper.SubmitPolicy policy)
This property holds the current submit policy. |
boolean |
submit()
Submits all changes from the mapped widgets to the model. |
QDataWidgetMapper.SubmitPolicy |
submitPolicy()
This property holds the current submit policy. |
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. |
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. |
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. |
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. |
Methods inherited from class com.trolltech.qt.core.QObject |
---|
childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, indexOfProperty, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, properties, property, removeEventFilter, setObjectName, setParent, setProperty, startTimer, timerEvent, toString, userProperty |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Field Detail |
---|
public final QSignalEmitter.Signal1 currentIndexChanged
This signal takes 1 generic argument(s). We list their type and the name they go by in the description of this signal. <java.lang.Integer(named: index)>:
This signal is emitted after the current index has changed and all widgets were populated with new data. index is the new current index.
currentIndex()
, and setCurrentIndex()
.
Constructor Detail |
---|
public QDataWidgetMapper()
setOrientation()
, and setSubmitPolicy()
.
public QDataWidgetMapper(QObject parent)
setOrientation()
, and setSubmitPolicy()
.
Method Detail |
---|
public final void addMapping(QWidget widget, int section)
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:
QDataWidgetMapper mapper = new QDataWidgetMapper(); mapper.setModel(model); mapper.addMapping(nameLineEdit, 0); mapper.addMapping(ageSpinBox, 1);Notes:
removeMapping()
, mappedSection()
, and clearMapping()
.
public final void addMapping(QWidget widget, int section, QByteArray propertyName)
addMapping()
, but adds the possibility to specify the property to use specifying propertyName. addMapping()
.
public final void clearMapping()
addMapping()
, and removeMapping()
.
public final int currentIndex()
setCurrentModelIndex()
, toFirst()
, toNext()
, toPrevious()
, and toLast()
.
public final QAbstractItemDelegate itemDelegate()
setItemDelegate()
.
public final QByteArray mappedPropertyName(QWidget widget)
mappedSection()
, addMapping()
, and removeMapping()
.
public final int mappedSection(QWidget widget)
addMapping()
, and removeMapping()
.
public final QWidget mappedWidgetAt(int section)
addMapping()
, and removeMapping()
.
public final QAbstractItemModel model()
setModel()
.
public final Qt.Orientation orientation()
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 | Nokia Corporation and/or its subsidiary(-ies) | Oslo |
2 | Trolltech Pty | Brisbane |
3 | Trolltech Inc | Silicon Valley |
4 | Trolltech China | Beijing |
5 | Trolltech GmbH | Berlin |
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 |
Nokia Corporation and/or its subsidiary(-ies) | Trolltech Pty | Trolltech Inc | Trolltech China | Trolltech GmbH |
Oslo | Brisbane | Silicon Valley | Beijing | Berlin |
public final void removeMapping(QWidget widget)
addMapping()
, and clearMapping()
.
public final void revert()
submit()
, and setSubmitPolicy()
.
public final com.trolltech.qt.core.QModelIndex rootIndex()
setRootIndex()
.
public final void setCurrentModelIndex(com.trolltech.qt.core.QModelIndex 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:
QDataWidgetMapper mapper = new QDataWidgetMapper(); myTableView.selectionModel().currentRowChanged.connect(mapper, "setCurrentModelIndex(QModelIndex)");
currentIndex()
.
public final void setItemDelegate(QAbstractItemDelegate delegate)
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.
itemDelegate()
.
public final void setModel(QAbstractItemModel model)
model()
.
public final void setOrientation(Qt.Orientation aOrientation)
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 | Nokia Corporation and/or its subsidiary(-ies) | Oslo |
2 | Trolltech Pty | Brisbane |
3 | Trolltech Inc | Silicon Valley |
4 | Trolltech China | Beijing |
5 | Trolltech GmbH | Berlin |
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 |
Nokia Corporation and/or its subsidiary(-ies) | Trolltech Pty | Trolltech Inc | Trolltech China | Trolltech GmbH |
Oslo | Brisbane | Silicon Valley | Beijing | Berlin |
public final void setRootIndex(com.trolltech.qt.core.QModelIndex index)
rootIndex()
.
public final void setSubmitPolicy(QDataWidgetMapper.SubmitPolicy policy)
public final boolean submit()
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.
revert()
, and setSubmitPolicy()
.
public final QDataWidgetMapper.SubmitPolicy submitPolicy()
public final void toFirst()
This is equivalent to calling setCurrentIndex(0).
toLast()
, and setCurrentIndex()
.
public final void toLast()
Calls setCurrentIndex()
internally.
toFirst()
, and setCurrentIndex()
.
public final void toNext()
Calls setCurrentIndex()
internally. Does nothing if there is no next row in the model.
toPrevious()
, and setCurrentIndex()
.
public final void toPrevious()
Calls setCurrentIndex()
internally. Does nothing if there is no previous row in the model.
toNext()
, and setCurrentIndex()
.
public void setCurrentIndex(int index)
setCurrentModelIndex()
, toFirst()
, toNext()
, toPrevious()
, and toLast()
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |