QStyledItemDelegate Class
The QStyledItemDelegate class provides display and editing facilities for data items from a model. More…
| Header | #include <QStyledItemDelegate> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QAbstractItemDelegate |
List of all members, including inherited members
Public Functions
| QStyledItemDelegate(QObject *parent = nullptr) | |
| ~QStyledItemDelegate() | |
| virtual QString | displayText(const QVariant &value, const QLocale &locale) const |
Reimplemented Public Functions
| void | paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| QSize | sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const |
| QWidget * | createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| void | setEditorData(QWidget *editor, const QModelIndex &index) const |
| void | setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const |
| void | updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const |
Protected Functions
| virtual void | initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const |
Reimplemented Protected Functions
| bool | eventFilter(QObject *object, QEvent *event) |
| bool | editorEvent(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:
Qt::DisplayRole: the text. Numbers, dates and times are written with displayText().
Qt::CheckStateRole: a check box before the text.
Qt::DecorationRole: a QPixmap, a QImage or a QColor before the text.
Qt::FontRole, Qt::ForegroundRole, Qt::BackgroundRole and Qt::TextAlignmentRole: the font, the colors and the alignment.
Qt::SizeHintRole: the size of the item, instead of the one that its contents need.
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
QStyledItemDelegate::QStyledItemDelegate(QObject *parent = nullptr)
Constructs a delegate with the given parent.
QStyledItemDelegate::~QStyledItemDelegate()
Destroys the delegate.
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.
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.
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.
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.
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.
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.
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().
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.
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.
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.