QAbstractItemDelegate Class
The QAbstractItemDelegate class is used to display and edit data items from a model. More...
| Header: | #include <QAbstractItemDelegate> |
| qmake: | QT += widgets |
| Inherits: | QObject |
| Inherited By: |
Public Types
| enum | EndEditHint { NoHint, EditNextItem, EditPreviousItem, SubmitModelCache, RevertModelCache } |
Detailed Description
A QAbstractItemDelegate provides the interface and common functionality for delegates in the model/view architecture. Delegates display individual items in views, and handle the editing of model data.
The QAbstractItemDelegate class is one of the Model/View Classes and is part of Qt's model/view framework.
To render an item in a custom way, you must implement paint() and sizeHint(). The QStyledItemDelegate class provides default implementations for these functions; if you do not need custom rendering, subclass that class instead.
We give an example of drawing a progress bar in items; in our case for a package management program.

We create the WidgetDelegate class, which inherits from QStyledItemDelegate. We do the drawing in the paint() function:
void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.column() == 1) { int progress = index.data().toInt(); QStyleOptionProgressBar progressBarOption; progressBarOption.rect = option.rect; progressBarOption.minimum = 0; progressBarOption.maximum = 100; progressBarOption.progress = progress; progressBarOption.text = QString::number(progress) + "%"; progressBarOption.textVisible = true; QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); } else QStyledItemDelegate::paint(painter, option, index);
Notice that we use a QStyleOptionProgressBar and initialize its members. We can then use the current QStyle to draw it.
To provide custom editing, there are two approaches that can be used. The first approach is to create an editor widget and display it directly on top of the item. To do this you must reimplement createEditor() to provide an editor widget, setEditorData() to populate the editor with the data from the model, and setModelData() so that the delegate can update the model with data from the editor.
The second approach is to handle user events directly by reimplementing editorEvent().
See also Model/View Programming, QStyledItemDelegate, Pixelator Example, QStyledItemDelegate, and QStyle.
Member Type Documentation
enum QAbstractItemDelegate::EndEditHint
This enum describes the different hints that the delegate can give to the model and view components to make editing data in a model a comfortable experience for the user.
| Constant | Value | Description |
|---|---|---|
QAbstractItemDelegate::NoHint | 0 | There is no recommended action to be performed. |
These hints let the delegate influence the behavior of the view:
| Constant | Value | Description |
|---|---|---|
QAbstractItemDelegate::EditNextItem | 1 | The view should use the delegate to open an editor on the next item in the view. |
QAbstractItemDelegate::EditPreviousItem | 2 | The view should use the delegate to open an editor on the previous item in the view. |
Note that custom views may interpret the concepts of next and previous differently.
The following hints are most useful when models are used that cache data, such as those that manipulate data locally in order to increase performance or conserve network bandwidth.
| Constant | Value | Description |
|---|---|---|
QAbstractItemDelegate::SubmitModelCache | 3 | If the model caches data, it should write out cached data to the underlying data store. |
QAbstractItemDelegate::RevertModelCache | 4 | If the model caches data, it should discard cached data and replace it with data from the underlying data store. |
Although models and views should respond to these hints in appropriate ways, custom components may ignore any or all of them if they are not relevant.