OGS
CheckboxDelegate Class Reference

Detailed Description

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 17 of file CheckboxDelegate.h.

#include <CheckboxDelegate.h>

Inheritance diagram for CheckboxDelegate:
[legend]
Collaboration diagram for CheckboxDelegate:
[legend]

Public Member Functions

 CheckboxDelegate (QObject *parent=nullptr)
 Constructor.
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
 Paints a checkbox. This overrides the default painting of a combo box.
bool editorEvent (QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
 Handles the click events and sets the model data.
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const override

Private Member Functions

QRect checkboxRect (const QStyleOptionViewItem &viewItemStyleOptions) const

Constructor & Destructor Documentation

◆ CheckboxDelegate()

CheckboxDelegate::CheckboxDelegate ( QObject * parent = nullptr)
explicit

Constructor.

Definition at line 14 of file CheckboxDelegate.cpp.

14: QItemDelegate(parent) {}

Member Function Documentation

◆ checkboxRect()

QRect CheckboxDelegate::checkboxRect ( const QStyleOptionViewItem & viewItemStyleOptions) const
private

Definition at line 92 of file CheckboxDelegate.cpp.

94{
95 QStyleOptionButton styleOptionButton;
96 QRect rect = QApplication::style()->subElementRect(
97 QStyle::SE_CheckBoxIndicator, &styleOptionButton);
98 QPoint point(viewItemStyleOptions.rect.x() +
99 viewItemStyleOptions.rect.width() / 2 - rect.width() / 2,
100 viewItemStyleOptions.rect.y() +
101 viewItemStyleOptions.rect.height() / 2 -
102 rect.height() / 2);
103 return QRect(point, rect.size());
104}

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 46 of file CheckboxDelegate.cpp.

49{
50 Q_UNUSED(option);
51
52 if ((event->type() == QEvent::MouseButtonRelease) ||
53 (event->type() == QEvent::MouseButtonDblClick))
54 {
55 auto* mouse_event = static_cast<QMouseEvent*>(event);
56 if (mouse_event->button() != Qt::LeftButton ||
57 !checkboxRect(option).contains(mouse_event->pos()))
58 {
59 return false;
60 }
61 if (event->type() == QEvent::MouseButtonDblClick)
62 {
63 return true;
64 }
65 }
66 else if (event->type() == QEvent::KeyPress)
67 {
68 if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&
69 static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
70 {
71 return false;
72 }
73 }
74 else
75 {
76 return false;
77 }
78
79 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
80 return model->setData(index, !checked, Qt::EditRole);
81}
QRect checkboxRect(const QStyleOptionViewItem &viewItemStyleOptions) const
constexpr bool contains()
Returns if Type is contained in the given List of types.
Definition TMP.h:158

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 16 of file CheckboxDelegate.cpp.

19{
20 if (index.isValid())
21 {
22 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
23
24 QStyleOptionButton styleOptionButton;
25 styleOptionButton.state |= QStyle::State_Enabled;
26 if (checked)
27 {
28 styleOptionButton.state |= QStyle::State_On;
29 }
30 else
31 {
32 styleOptionButton.state |= QStyle::State_Off;
33 }
34
35 styleOptionButton.rect = this->checkboxRect(option);
36
37 QApplication::style()->drawControl(QStyle::CE_CheckBox,
38 &styleOptionButton, painter);
39 }
40 else
41 {
42 QItemDelegate::paint(painter, option, index);
43 }
44}

References checkboxRect().

◆ sizeHint()

QSize CheckboxDelegate::sizeHint ( const QStyleOptionViewItem & option,
const QModelIndex & index ) const
override

Definition at line 83 of file CheckboxDelegate.cpp.

85{
86 Q_UNUSED(index);
87
88 QRect rect = checkboxRect(option);
89 return QSize(rect.width(), rect.height());
90}

References checkboxRect().


The documentation for this class was generated from the following files: