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

25: QItemDelegate(parent) {}

Member Function Documentation

◆ checkboxRect()

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

Definition at line 103 of file CheckboxDelegate.cpp.

105{
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 -
113 rect.height() / 2);
114 return QRect(point, rect.size());
115}

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.

60{
61 Q_UNUSED(option);
62
63 if ((event->type() == QEvent::MouseButtonRelease) ||
64 (event->type() == QEvent::MouseButtonDblClick))
65 {
66 auto* mouse_event = static_cast<QMouseEvent*>(event);
67 if (mouse_event->button() != Qt::LeftButton ||
68 !checkboxRect(option).contains(mouse_event->pos()))
69 {
70 return false;
71 }
72 if (event->type() == QEvent::MouseButtonDblClick)
73 {
74 return true;
75 }
76 }
77 else if (event->type() == QEvent::KeyPress)
78 {
79 if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&
80 static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
81 {
82 return false;
83 }
84 }
85 else
86 {
87 return false;
88 }
89
90 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
91 return model->setData(index, !checked, Qt::EditRole);
92}
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.

30{
31 if (index.isValid())
32 {
33 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
34
35 QStyleOptionButton styleOptionButton;
36 styleOptionButton.state |= QStyle::State_Enabled;
37 if (checked)
38 {
39 styleOptionButton.state |= QStyle::State_On;
40 }
41 else
42 {
43 styleOptionButton.state |= QStyle::State_Off;
44 }
45
46 styleOptionButton.rect = this->checkboxRect(option);
47
48 QApplication::style()->drawControl(QStyle::CE_CheckBox,
49 &styleOptionButton, painter);
50 }
51 else
52 {
53 QItemDelegate::paint(painter, option, index);
54 }
55}

References checkboxRect().

◆ sizeHint()

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

Definition at line 94 of file CheckboxDelegate.cpp.

96{
97 Q_UNUSED(index);
98
99 QRect rect = checkboxRect(option);
100 return QSize(rect.width(), rect.height());
101}

References checkboxRect().


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