QAbstractItemDelegate Class
The QAbstractItemDelegate class is used to display and edit data items from a model. More…
| Header | #include <QAbstractItemDelegate> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QObject |
| Inherited by | QStyledItemDelegate |
List of all members, including inherited members
Public Types
| enum | EndEditHint |
Public Functions
| QAbstractItemDelegate(QObject *parent = nullptr) | |
| ~QAbstractItemDelegate() | |
| virtual void | paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| virtual QSize | sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
| virtual QWidget * | createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| virtual void | destroyEditor(QWidget *editor, const QModelIndex &index) const |
| virtual void | setEditorData(QWidget *editor, const QModelIndex &index) const |
| virtual void | setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const |
| virtual void | updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| virtual bool | editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) |
Signals
| void | commitData(QWidget *editor) |
| void | closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint) |
| void | sizeHintChanged(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.
| Constant | Description |
|---|---|
QAbstractItemDelegate::NoHint | Nothing in particular. |
QAbstractItemDelegate::EditNextItem | Open an editor on the next item. |
QAbstractItemDelegate::EditPreviousItem | Open an editor on the item before. |
QAbstractItemDelegate::SubmitModelCache | The data was written to the model. |
QAbstractItemDelegate::RevertModelCache | The edit was given up; the model keeps what it had. |
Member Function Documentation
QAbstractItemDelegate::QAbstractItemDelegate(QObject *parent = nullptr)
Constructs a delegate with the given parent.
QAbstractItemDelegate::~QAbstractItemDelegate()
Destroys the delegate.
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.
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.
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().
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().
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.
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.
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().
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().
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.
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.
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.