74 lines
2.1 KiB
C++
74 lines
2.1 KiB
C++
#include "checkboxdelegate.h"
|
|
#include <QCheckBox>
|
|
|
|
CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
|
|
: QItemDelegate(parent)
|
|
{
|
|
|
|
}
|
|
|
|
CheckBoxDelegate::~CheckBoxDelegate()
|
|
{
|
|
|
|
}
|
|
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
const QModelIndex &index) const
|
|
{
|
|
QItemDelegate::paint(painter, option, index);
|
|
}
|
|
|
|
QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
return QItemDelegate::sizeHint(option, index);
|
|
}
|
|
|
|
QWidget *CheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
|
const QModelIndex &index) const
|
|
{
|
|
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
|
{
|
|
QCheckBox *editor = new QCheckBox(parent);
|
|
editor->installEventFilter(const_cast<CheckBoxDelegate *>(this));
|
|
return editor;
|
|
}
|
|
else
|
|
{
|
|
return QItemDelegate::createEditor(parent, option, index);
|
|
}
|
|
}
|
|
|
|
void CheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
|
{
|
|
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
|
{
|
|
QString value = index.model()->data(index, Qt::DisplayRole).toString();
|
|
QCheckBox *checkbox = static_cast<QCheckBox *>(editor);
|
|
if (value.toInt() == 1)
|
|
checkbox->setCheckState(Qt::Checked);
|
|
else
|
|
checkbox->setCheckState(Qt::Unchecked);
|
|
}
|
|
else
|
|
{
|
|
QItemDelegate::setEditorData(editor, index);
|
|
}
|
|
}
|
|
|
|
void CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
|
const QModelIndex &index) const
|
|
{
|
|
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
|
{
|
|
QCheckBox *checkbox = static_cast<QCheckBox *>(editor);
|
|
|
|
if (checkbox->isChecked())
|
|
model->setData(index, 1);
|
|
else
|
|
model->setData(index, 0);
|
|
}
|
|
else
|
|
{
|
|
QItemDelegate::setModelData(editor, model, index);
|
|
}
|
|
}
|