#include "checkboxdelegate.h" #include #include #include #include #include CheckBoxDelegate::CheckBoxDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem viewItemOption(option); if (option.state & QStyle::State_Selected) { painter->setBrush(option.palette.highlight()); QPen nPen; nPen.setColor(option.palette.highlight().color()); painter->setPen(nPen); painter->drawRect(option.rect); } const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; QRect newRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, QSize(option.decorationSize.width() - 1.5 * textMargin, option.decorationSize.height()), QRect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height())); viewItemOption.rect = newRect; QStyledItemDelegate::paint(painter, viewItemOption, index); } bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { Q_ASSERT(event); Q_ASSERT(model); // make sure that the item is checkable Qt::ItemFlags flags = model->flags(index); if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled)) return false; // make sure that we have a check state QVariant value = index.data(Qt::CheckStateRole); if (!value.isValid()) return false; // make sure that we have the right event type if (event->type() == QEvent::MouseButtonRelease) { QRect checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter, option.decorationSize, QRect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height())); if (!checkRect.contains(static_cast(event)->pos())) return false; } else if (event->type() == QEvent::KeyPress) { if (static_cast(event)->key() != Qt::Key_Space && static_cast(event)->key() != Qt::Key_Select) return false; } else { return false; } Qt::CheckState state = (static_cast(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked); return model->setData(index, state, Qt::CheckStateRole); }