6.0.0-rc4Source

QStyledItemDelegate Class

The QStyledItemDelegate class provides display and editing facilities for data items from a model. More…

Header#include <QStyledItemDelegate>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQAbstractItemDelegate

List of all members, including inherited members

Public Functions

QStyledItemDelegate(QObject *parent = nullptr)
~QStyledItemDelegate()
virtual QStringdisplayText(const QVariant &value, const QLocale &locale) const

Reimplemented Public Functions

voidpaint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QSizesizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
voidsetEditorData(QWidget *editor, const QModelIndex &index) const
voidsetModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
voidupdateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

Protected Functions

virtual voidinitStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const

Reimplemented Protected Functions

booleventFilter(QObject *object, QEvent *event)
booleditorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)

Detailed Description

This is the delegate that the views of Nt Widgets use unless they are given another one. It draws an item from what the model returns for the roles of the item:

For editing it makes a widget that fits the type of the value of Qt::EditRole: a combo box for a bool, a spin box for an integer, a QDoubleSpinBox for a double, a QDateEdit, QTimeEdit or QDateTimeEdit for a date or a time, and a QLineEdit for everything else. Return or Enter in the editor stores the value, Escape gives the edit up, and Tab stores it and moves on to the next item; so does moving the focus elsewhere. A click on the check box of an item that is Qt::ItemIsUserCheckable toggles it without an editor.

To draw or edit some items in another way, subclass the delegate and reimplement paint() and sizeHint(), or createEditor(), setEditorData() and setModelData():

class RatingDelegate : public QStyledItemDelegate {
public:
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const override {
        auto *box = new QSpinBox(parent);
        box->setRange(0, 5);
        return box;
    }
};

view->setItemDelegateForColumn(2, new RatingDelegate);

See also QAbstractItemDelegate and QTreeView.

Differences from Qt

The item is drawn by the delegate itself, not by the style, so a custom QStyle does not change how items look. The decoration is a pixmap, an image or a color: Nt has no QIcon. Text is drawn on one line. There is no QItemEditorFactory: the editor for a type is chosen by createEditor(). QItemDelegate is not provided.

Member Function Documentation

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

Constructs a delegate with the given parent.

[override] QStyledItemDelegate::~QStyledItemDelegate()

Destroys the delegate.

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

Reimplements QAbstractItemDelegate::createEditor().

Returns an editor for the item index as a child of parent, of the kind that fits the type of the value that the model returns for Qt::EditRole; see the description of the class. option is not used.

[virtual] QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale &locale) const

Returns the text that the value value is shown as, written the way locale writes numbers, dates and times. A subclass reimplements this to show values in another way, with a unit for example.

[override, protected] bool QStyledItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)

Reimplements QAbstractItemDelegate::editorEvent().

Toggles the check box of the item index of model when it is clicked, or when Space is pressed on the item, if the item is enabled and Qt::ItemIsUserCheckable; option has the rectangle of the item. Returns true if event did that.

[override, protected] bool QStyledItemDelegate::eventFilter(QObject *object, QEvent *event)

Reimplements QAbstractItemDelegate::eventFilter().

Watches the editor object: Return and Enter in it store its value and close it, Escape closes it without storing, Tab and Shift+Tab store and move to the next and the previous item, and so does the editor losing the focus to another widget of its window. Other event are left to the editor.

[virtual, protected] void QStyledItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const

Fills option with what the model says about the item index: its text, check state, decoration, font, colors and alignment. paint() and sizeHint() call this; a subclass that reimplements them calls it too, or reimplements it to change what they see.

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

Reimplements QAbstractItemDelegate::paint().

Draws the item index with painter in the rectangle of option: its background, or the highlight if it is selected, then its check box, its decoration and its text.

[override] void QStyledItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const

Reimplements QAbstractItemDelegate::setEditorData().

Shows the value that the model returns for Qt::EditRole of the item index in editor, one of the editors of createEditor().

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

Reimplements QAbstractItemDelegate::setModelData().

Writes what editor shows to Qt::EditRole of the item index of model, as a value of the type that the item had.

[override] QSize QStyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

Reimplements QAbstractItemDelegate::sizeHint().

Returns the size that the item index needs: what the model gives for Qt::SizeHintRole, or the size of its check box, decoration and text in the font of option.

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

Reimplements QAbstractItemDelegate::updateEditorGeometry().

Gives editor the rectangle of the item in option, less the room that the check box and the decoration of the item index take.