6.0.0-rc4Source

QAbstractItemDelegate Class

The QAbstractItemDelegate class is used to display and edit data items from a model. More…

Header#include <QAbstractItemDelegate>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQObject
Inherited byQStyledItemDelegate

List of all members, including inherited members

Public Types

Public Functions

QAbstractItemDelegate(QObject *parent = nullptr)
~QAbstractItemDelegate()
virtual voidpaint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual QSizesizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual voiddestroyEditor(QWidget *editor, const QModelIndex &index) const
virtual voidsetEditorData(QWidget *editor, const QModelIndex &index) const
virtual voidsetModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
virtual voidupdateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual booleditorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)

Signals

voidcommitData(QWidget *editor)
voidcloseEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint)
voidsizeHintChanged(const QModelIndex &index)

Detailed Description

A view does not draw its items itself: it asks its delegate, for each item, to paint() it and to tell its sizeHint(). The delegate also makes the widget that an item is edited with: createEditor() makes it, setEditorData() fills it from the model, updateEditorGeometry() places it over the item, and setModelData() writes what the user entered back to the model. When the user is done, the delegate emits commitData() and closeEditor().

Subclass QStyledItemDelegate, which does all this for the usual kinds of data, to change how some items look or are edited; subclass this class to do it all from scratch.

See also QStyledItemDelegate and QAbstractItemView.

Differences from Qt

There is no helpEvent(): the view shows the tool tips of its items.

Member Type Documentation

enum QAbstractItemDelegate::EndEditHint

What a view should do after an editor was closed.

ConstantDescription
QAbstractItemDelegate::NoHintNothing in particular.
QAbstractItemDelegate::EditNextItemOpen an editor on the next item.
QAbstractItemDelegate::EditPreviousItemOpen an editor on the item before.
QAbstractItemDelegate::SubmitModelCacheThe data was written to the model.
QAbstractItemDelegate::RevertModelCacheThe edit was given up; the model keeps what it had.

Member Function Documentation

[explicit] QAbstractItemDelegate::QAbstractItemDelegate(QObject *parent = nullptr)

Constructs a delegate with the given parent.

[override] QAbstractItemDelegate::~QAbstractItemDelegate()

Destroys the delegate.

[signal] void QAbstractItemDelegate::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint)

This signal is emitted when the user has finished with editor. The view removes the editor, and hint tells it what to do next.

[signal] void QAbstractItemDelegate::commitData(QWidget *editor)

This signal is emitted when the data of editor should be written to the model. The view calls setModelData() for it.

[virtual] QWidget * QAbstractItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const

Returns a new widget, a child of parent, for editing the item index, or nullptr if the item cannot be edited; option has the font and the rectangle of the item. The view owns the editor. The base implementation returns nullptr.

See also setEditorData(), setModelData() and destroyEditor().

[virtual] void QAbstractItemDelegate::destroyEditor(QWidget *editor, const QModelIndex &index) const

Called when the editor editor of the item index is no longer needed. The base implementation deletes it when control returns to the event loop; a delegate that keeps its editors for reuse overrides this.

See also createEditor().

[virtual] bool QAbstractItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)

Called by the view with a mouse or key event on the item index of model, before the view starts an editor for it; option has the rectangle of the item. A delegate handles here what needs no editor, such as a click on a check box. Returns true if the event was handled, which the base implementation never does.

[virtual] void QAbstractItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

Draws the item index with painter, in the rectangle and with the state, palette and font of option.

[virtual] void QAbstractItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const

Gives editor the data of the item index to show and edit. The base implementation does nothing.

See also setModelData().

[virtual] void QAbstractItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const

Writes the contents of editor to the item index of model. The base implementation does nothing.

See also setEditorData().

[virtual] QSize QAbstractItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

Returns the size that the item index needs to show its data, with the font and the other settings of option.

[signal] void QAbstractItemDelegate::sizeHintChanged(const QModelIndex &index)

This signal is emitted when the size hint of the item index changed, so that a view lays its items out again.

[virtual] void QAbstractItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

Places editor over the item index, whose rectangle is in option. The base implementation does nothing.