I would like to personalize the 3rd columns to contain QCheckBox widgets. I would like those QCheckBox widgets to be customized with three icons: 1.png (Default state) | 2.png (Selected state) | 3.png (Disabled state). So I could do this using a custom delegate with the following implementation:
#include "mydelegate.h"
#include <QCheckBox>
#include <QPainter>
#include <QKeyEvent>
#include <QtDebug>
#include <QApplication>
#include <QStyleOptionViewItem>
MyDelegate::MyDelegate(QObject* parent) :
QStyledItemDelegate(parent)
{
// 1.png
_icon.addPixmap(QPixmap("1.png"), QIcon::Active, QIcon::On);
// 2.png
_icon.addPixmap(QPixmap("2.png"), QIcon::Selected, QIcon::On);
// 3.png
_icon.addPixmap(QPixmap("3.png"), QIcon::Disabled, QIcon::On);
}
void MyDelegate::paint(QPainter* painter, const QStyleOptionViewItem&
option, const QModelIndex& index) const
{
if (index.column() != 2)
QStyledItemDelegate::paint(painter, option, index);
else
{
bool value = index.model()->data(index,
Qt::UserRole).toBool();
QStyleOptionButton buttonVis;
buttonVis.rect = option.rect;
buttonVis.iconSize = QSize(50, 50);
buttonVis.icon = _icon;
buttonVis.features |= QStyleOptionButton::Flat;
buttonVis.state |= value ? QStyle::State_Enabled : QStyle::State_None;
QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonVis, painter);
}
}
bool MyDelegate::editorEvent(QEvent* event, QAbstractItemModel* model,
const QStyleOptionViewItem& option, const QModelIndex& index)
{
if (event->type() == QEvent::MouseButtonRelease)
{
bool value = model->data(index, Qt::UserRole).toBool();
model->setData(index, !value, Qt::UserRole);
}
return true;
}
The problem that it's work for 1.png and 3.png and not for the 2.png. i want that it will work for the three icons

