17 const QStyleOptionViewItem& option,
18 const QModelIndex& index)
const
22 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
24 QStyleOptionButton styleOptionButton;
25 styleOptionButton.state |= QStyle::State_Enabled;
28 styleOptionButton.state |= QStyle::State_On;
32 styleOptionButton.state |= QStyle::State_Off;
37 QApplication::style()->drawControl(QStyle::CE_CheckBox,
38 &styleOptionButton, painter);
42 QItemDelegate::paint(painter, option, index);
47 const QStyleOptionViewItem& option,
48 const QModelIndex& index)
52 if ((event->type() == QEvent::MouseButtonRelease) ||
53 (event->type() == QEvent::MouseButtonDblClick))
55 auto* mouse_event =
static_cast<QMouseEvent*
>(event);
56 if (mouse_event->button() != Qt::LeftButton ||
61 if (event->type() == QEvent::MouseButtonDblClick)
66 else if (event->type() == QEvent::KeyPress)
68 if (
static_cast<QKeyEvent*
>(event)->key() != Qt::Key_Space &&
69 static_cast<QKeyEvent*
>(event)->key() != Qt::Key_Select)
79 bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
80 return model->setData(index, !checked, Qt::EditRole);
93 const QStyleOptionViewItem& viewItemStyleOptions)
const
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 -
103 return QRect(point, rect.size());
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.