Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QDataWidgetMapper Class

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

Header:#include <QDataWidgetMapper>
Since: Qt 4.2
Inherits:QObject

Public Types

enumSubmitPolicy { AutoSubmit, ManualSubmit }

Properties

Public Functions

QDataWidgetMapper(QObject * parent = 0)
~QDataWidgetMapper()
voidaddMapping(QWidget * widget, int section)
voidaddMapping(QWidget * widget, int section, const QByteArray & propertyName)
voidclearMapping()
intcurrentIndex() const
QAbstractItemDelegate *itemDelegate() const
QByteArraymappedPropertyName(QWidget * widget) const
intmappedSection(QWidget * widget) const
QWidget *mappedWidgetAt(int section) const
QAbstractItemModel *model() const
Qt::Orientationorientation() const
voidremoveMapping(QWidget * widget)
QModelIndexrootIndex() const
voidsetItemDelegate(QAbstractItemDelegate * delegate)
voidsetModel(QAbstractItemModel * model)
voidsetOrientation(Qt::Orientation aOrientation)
voidsetRootIndex(const QModelIndex & index)
voidsetSubmitPolicy(SubmitPolicy policy)
SubmitPolicysubmitPolicy() const
  • 29 public functions inherited fromQObject

Public Slots

voidrevert()
virtual voidsetCurrentIndex(int index)
voidsetCurrentModelIndex(const QModelIndex & index)
boolsubmit()
voidtoFirst()
voidtoLast()
voidtoNext()
voidtoPrevious()
  • 1 public slot inherited fromQObject

Signals

voidcurrentIndexChanged(int index)

Additional Inherited Members

  • 7 static public members inherited fromQObject
  • 8 protected functions inherited fromQObject

Detailed Description

TheQDataWidgetMapper 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'suser property is used to transfer data between the model and the widget. Since Qt 4.3, an additionaladdMapping() 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, aQItemDelegate is used to synchronize the model with the widgets.

Let us assume that we have an item model namedmodel with the following contents:

1Qt NorwayOslo
2Qt AustraliaBrisbane
3Qt USAPalo Alto
4Qt ChinaBeijing
5Qt GermanyBerlin

The following code will map the columns of the model to widgets calledmySpinBox,myLineEdit andmyCountryChooser:

QDataWidgetMapper*mapper=newQDataWidgetMapper;mapper->setModel(model);mapper->addMapping(mySpinBox,0);mapper->addMapping(myLineEdit,1);mapper->addMapping(myCountryChooser,2);mapper->toFirst();

After the call totoFirst(),mySpinBox displays the value1,myLineEdit displaysQt Norway andmyCountryChooser displaysOslo. The navigational functionstoFirst(),toNext(),toPrevious(),toLast() andsetCurrentIndex() can be used to navigate in the model and update the widgets with contents from the model.

ThesetRootIndex() 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 andManualSubmit.AutoSubmit will update the model as soon as the current widget loses focus,ManualSubmit will not update the model unlesssubmit() 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 thatQDataWidgetMapper 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 alsoQAbstractItemModel andQAbstractItemDelegate.

Member Type Documentation

enum QDataWidgetMapper::SubmitPolicy

This enum describes the possible submit policies aQDataWidgetMapper 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 untilsubmit() is called.

Property Documentation

currentIndex :int

This property holds the current row or column.

The widgets are populated with with data from the row atindex if the orientation is horizontal (the default), otherwise with data from the column atindex.

Access functions:

intcurrentIndex() const
virtual voidsetCurrentIndex(int index)

Notifier signal:

voidcurrentIndexChanged(int index)

See alsosetCurrentModelIndex(),toFirst(),toNext(),toPrevious(), andtoLast().

orientation :Qt::Orientation

This property holds the orientation of the model.

If the orientation isQt::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 thatcurrentIndex() points at.

UseQt::Horizontal for tabular data that looks like this:

1Qt NorwayOslo
2Qt AustraliaBrisbane
3Qt USASilicon Valley
4Qt ChinaBeijing
5Qt GermanyBerlin

If the orientation is set toQt::Vertical, a widget is mapped to a row. CallingsetCurrentIndex() will change the current column. The widget will be populates with the model's data from its mapped row and the column thatcurrentIndex() points at.

UseQt::Vertical for tabular data that looks like this:

12345
Qt NorwayQt AustraliaQt USAQt ChinaQt Germany
OsloBrisbaneSilicon ValleyBeijingBerlin

Changing the orientation clears all existing mappings.

Access functions:

Qt::Orientationorientation() const
voidsetOrientation(Qt::Orientation aOrientation)

submitPolicy :SubmitPolicy

This property holds the current submit policy.

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

Access functions:

SubmitPolicysubmitPolicy() const
voidsetSubmitPolicy(SubmitPolicy policy)

Member Function Documentation

QDataWidgetMapper::QDataWidgetMapper(QObject * parent = 0)

Constructs a newQDataWidgetMapper with parent objectparent. By default, the orientation is horizontal and the submit policy isAutoSubmit.

See alsosetOrientation() andsetSubmitPolicy().

QDataWidgetMapper::~QDataWidgetMapper()

Destroys the object.

void QDataWidgetMapper::addMapping(QWidget * widget,int section)

Adds a mapping between awidget and asection from the model. Thesection is a column in the model if the orientation is horizontal (the default), otherwise a row.

For the following example, we assume a modelmyModel 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 theQLineEditnameLineEdit, and the second is mapped to theQSpinBoxageSpinBox:

QDataWidgetMapper*mapper=newQDataWidgetMapper();mapper->setModel(myModel);mapper->addMapping(nameLineEdit,0);mapper->addMapping(ageSpinBox,1);

Notes:

  • If thewidget 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 alsoremoveMapping(),mappedSection(), andclearMapping().

void QDataWidgetMapper::addMapping(QWidget * widget,int section, constQByteArray & propertyName)

Essentially the same asaddMapping(), but adds the possibility to specify the property to use specifyingpropertyName.

This function was introduced in Qt 4.3.

See alsoaddMapping().

void QDataWidgetMapper::clearMapping()

Clears all mappings.

See alsoaddMapping() andremoveMapping().

QAbstractItemDelegate * QDataWidgetMapper::itemDelegate() const

Returns the current item delegate.

See alsosetItemDelegate().

QByteArray QDataWidgetMapper::mappedPropertyName(QWidget * widget) const

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

This function was introduced in Qt 4.3.

See alsomappedSection(),addMapping(), andremoveMapping().

int QDataWidgetMapper::mappedSection(QWidget * widget) const

Returns the section thewidget is mapped to or -1 if the widget is not mapped.

See alsoaddMapping() andremoveMapping().

QWidget * QDataWidgetMapper::mappedWidgetAt(int section) const

Returns the widget that is mapped atsection, or 0 if no widget is mapped at that section.

See alsoaddMapping() andremoveMapping().

QAbstractItemModel * QDataWidgetMapper::model() const

Returns the current model.

See alsosetModel().

void QDataWidgetMapper::removeMapping(QWidget * widget)

Removes the mapping for the givenwidget.

See alsoaddMapping() andclearMapping().

[slot]void QDataWidgetMapper::revert()

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

See alsosubmit() andsetSubmitPolicy().

QModelIndex QDataWidgetMapper::rootIndex() const

Returns the current root index.

See alsosetRootIndex().

[slot]void QDataWidgetMapper::setCurrentModelIndex(constQModelIndex & index)

Sets the current index to the row of theindex if the orientation is horizontal (the default), otherwise to the column of theindex.

CallssetCurrentIndex() internally. This convenience slot can be connected to the signalcurrentRowChanged() orcurrentColumnChanged() of another view'sselection model.

The following example illustrates how to update all widgets with new data whenever the selection of aQTableView namedmyTableView changes:

QDataWidgetMapper*mapper=newQDataWidgetMapper();connect(myTableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),        mapper, SLOT(setCurrentModelIndex(QModelIndex)));

See alsocurrentIndex().

void QDataWidgetMapper::setItemDelegate(QAbstractItemDelegate * delegate)

Sets the item delegate todelegate. The delegate will be used to write data from the model into the widget and from the widget to the model, usingQAbstractItemDelegate::setEditorData() andQAbstractItemDelegate::setModelData().

The delegate also decides when to apply data and when to change the editor, usingQAbstractItemDelegate::commitData() andQAbstractItemDelegate::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 thecloseEditor() signal, and attempt to access, modify or close an editor that has already been closed.

See alsoitemDelegate().

void QDataWidgetMapper::setModel(QAbstractItemModel * model)

Sets the current model tomodel. If another model was set, all mappings to that old model are cleared.

See alsomodel().

void QDataWidgetMapper::setRootIndex(constQModelIndex & index)

Sets the root item toindex. This can be used to display a branch of a tree. Pass an invalid model index to display the top-most branch.

See alsorootIndex().

[slot]bool QDataWidgetMapper::submit()

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'ssubmit() 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 alsorevert() andsetSubmitPolicy().

[slot]void QDataWidgetMapper::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.

This is equivalent to callingsetCurrentIndex(0).

See alsotoLast() andsetCurrentIndex().

[slot]void QDataWidgetMapper::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.

CallssetCurrentIndex() internally.

See alsotoFirst() andsetCurrentIndex().

[slot]void QDataWidgetMapper::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.

CallssetCurrentIndex() internally. Does nothing if there is no next row in the model.

See alsotoPrevious() andsetCurrentIndex().

[slot]void QDataWidgetMapper::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.

CallssetCurrentIndex() internally. Does nothing if there is no previous row in the model.

See alsotoNext() andsetCurrentIndex().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp