GimpMaster
18th May 2010, 15:29
Hello, I believe I have read through most of the posts on QCheckbox's and QTableviews. It seems most people prefer to use a custom delegate which makes sense. I have used the spinboxdelegate as an example and I was successful in getting the QCheckbox to draw when entering the editrole of the table view, however when not in the edit role (i.e. paint()) function I have not yet figured out how to successfully draw/render the QCheckbox. Here are my two questions with code to follow:
1. How can i make it so that a single click will toggle the checkbox as one would expect, instead of a double click to start editing and then a third click to toggle the checkbox?
2. What am I doing wrong in my paint function?
Thanks in advance!
void cCheckboxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
bool Checked = index.model()->data(index, Qt::EditRole).toBool();
QCheckBox CheckBox;
CheckBox.setChecked(Checked);
CheckBox.render(painter);
}
QSize cCheckboxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QCheckBox CheckBox;
return CheckBox.sizeHint();
}
QWidget *cCheckboxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QCheckBox *editor = new QCheckBox(parent);
return editor;
}
void cCheckboxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
bool checked = index.model()->data(index, Qt::EditRole).toBool();
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
checkBox->setChecked(checked);
}
void cCheckboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
bool checked = checkBox->isChecked();
model->setData(index, checked, Qt::EditRole);
}
void cCheckboxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
1. How can i make it so that a single click will toggle the checkbox as one would expect, instead of a double click to start editing and then a third click to toggle the checkbox?
2. What am I doing wrong in my paint function?
Thanks in advance!
void cCheckboxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
bool Checked = index.model()->data(index, Qt::EditRole).toBool();
QCheckBox CheckBox;
CheckBox.setChecked(Checked);
CheckBox.render(painter);
}
QSize cCheckboxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QCheckBox CheckBox;
return CheckBox.sizeHint();
}
QWidget *cCheckboxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QCheckBox *editor = new QCheckBox(parent);
return editor;
}
void cCheckboxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
bool checked = index.model()->data(index, Qt::EditRole).toBool();
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
checkBox->setChecked(checked);
}
void cCheckboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
bool checked = checkBox->isChecked();
model->setData(index, checked, Qt::EditRole);
}
void cCheckboxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}