CheckboxDelegate modifies a model view to display boolean values as checkboxes.
Important: the column on which this delegate is set (QAbstractItemView::setItemDelegateForColumn()) must not have the flags Qt::ItemIsEditable or Qt::ItemIsUserCheckable set in the model.
Definition at line 28 of file CheckboxDelegate.h.
#include <CheckboxDelegate.h>
|
| CheckboxDelegate (QObject *parent=nullptr) |
| Constructor. More...
|
|
void | paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override |
| Paints a checkbox. This overrides the default painting of a combo box. More...
|
|
bool | editorEvent (QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override |
| Handles the click events and sets the model data. More...
|
|
QSize | sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const override |
|
|
QRect | checkboxRect (const QStyleOptionViewItem &viewItemStyleOptions) const |
|
◆ CheckboxDelegate()
CheckboxDelegate::CheckboxDelegate |
( |
QObject * |
parent = nullptr | ) |
|
|
explicit |
◆ checkboxRect()
QRect CheckboxDelegate::checkboxRect |
( |
const QStyleOptionViewItem & |
viewItemStyleOptions | ) |
const |
|
private |
Definition at line 103 of file CheckboxDelegate.cpp.
106 QStyleOptionButton styleOptionButton;
107 QRect rect = QApplication::style()->subElementRect(
108 QStyle::SE_CheckBoxIndicator, &styleOptionButton);
109 QPoint point(viewItemStyleOptions.rect.x() +
110 viewItemStyleOptions.rect.width() / 2 - rect.width() / 2,
111 viewItemStyleOptions.rect.y() +
112 viewItemStyleOptions.rect.height() / 2 -
114 return QRect(point, rect.size());
Referenced by editorEvent(), paint(), and sizeHint().
◆ editorEvent()
bool CheckboxDelegate::editorEvent |
( |
QEvent * |
event, |
|
|
QAbstractItemModel * |
model, |
|
|
const QStyleOptionViewItem & |
option, |
|
|
const QModelIndex & |
index |
|
) |
| |
|
override |
Handles the click events and sets the model data.
Definition at line 57 of file CheckboxDelegate.cpp.
63 if ((event->type() == QEvent::MouseButtonRelease) ||
64 (event->type() == QEvent::MouseButtonDblClick))
66 auto* mouse_event =
static_cast<QMouseEvent*
>(event);
67 if (mouse_event->button() != Qt::LeftButton ||
72 if (event->type() == QEvent::MouseButtonDblClick)
77 else if (event->type() == QEvent::KeyPress)
79 if (
static_cast<QKeyEvent*
>(event)->key() != Qt::Key_Space &&
80 static_cast<QKeyEvent*
>(event)->key() != Qt::Key_Select)
90 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
91 return model->setData(index, !checked, Qt::EditRole);
QRect checkboxRect(const QStyleOptionViewItem &viewItemStyleOptions) const
References checkboxRect().
◆ paint()
void CheckboxDelegate::paint |
( |
QPainter * |
painter, |
|
|
const QStyleOptionViewItem & |
option, |
|
|
const QModelIndex & |
index |
|
) |
| const |
|
override |
Paints a checkbox. This overrides the default painting of a combo box.
Definition at line 27 of file CheckboxDelegate.cpp.
33 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
35 QStyleOptionButton styleOptionButton;
36 styleOptionButton.state |= QStyle::State_Enabled;
39 styleOptionButton.state |= QStyle::State_On;
43 styleOptionButton.state |= QStyle::State_Off;
48 QApplication::style()->drawControl(QStyle::CE_CheckBox,
49 &styleOptionButton, painter);
53 QItemDelegate::paint(painter, option, index);
References checkboxRect().
◆ sizeHint()
QSize CheckboxDelegate::sizeHint |
( |
const QStyleOptionViewItem & |
option, |
|
|
const QModelIndex & |
index |
|
) |
| const |
|
override |
The documentation for this class was generated from the following files: