Qt-interest Archive, April 2008
Custom delegate for showing QCheckBoxes
Message 1 in thread
Hi again,
I am looking for the simplest way to make my QTableView shows QCheckBox
as editors and view for boolean data.
I have gone through the length of creating custom delegate (by subclassing
QItemDelegate) and plug it to my view. But still not quite yet getting
what i need.
Boolean values are still shown as true/false (as plain text). Only
when I edit a
value the checkbox comes out.
What else should I do? I read the color editor examples from qt docs but
it involves creating a whole new widget for the delegate to show. I figured
that this is too much just to expose a common data type (boolean) to
which a perfectly suitable widget (QCheckBox) is already there.
Here'are the only two overridden functions in my delegate class:
//---
// class CustomDelegate : public QItemDelegate
QWidget* CustomDelegate::createEditor(QWidget *p,
const QStyleOptionViewItem& opt,
const QModelIndex& index) const
{
if (index.data().canConvert(QVariant::Bool)
return new QCheckBox();
else
return QItemDelegate::createEditor(p, opt, index);
}
QWidget* CustomDelegate::setEditorData(
QWidget *editorWidget,
const QModelIndex &index) const
{
if (index.data().canConvert(QVariant::Bool)
{ QCheckBox* editor = static_cast<QCheckbox*>(editorWidget);
editor->setChecked(index.data().toBool());
}
else
return QItemDelegate::setEditorData(editorWidget,index);
}
//---
should I also override paint()?
Regards,
--
[ signature omitted ]