john_erlandsson
19th January 2012, 03:37
Hi!
I have subclassed a QSqlQueryModel and a QItemDelegate. Making a column containing a date editable, and displayed as a QCheckBox. Cell contains a date = checked cell is null = not checked. Checking a cell = insert todays date, unchecking a cell = set date to "".
The only thing left to do is to allways display the checkbox delegate, even when not editing. After a bit of googling I found out that i have to reimplement the paint() function in my QItemDelegate, to display a CheckBox instead of text.
The problem is that i have no idea where to begin. The manual gives a short example, but it doesent make much sense to me.
Does anyone know of a better example?
The implementation of my QItemDelegate (Basically yanked from the SpinBoxDelegate example):
#include "economyhoursattdelegate.h"
#include <QCheckBox>
#include <QDate>
/* Constructor
*
*/
EconomyHoursAttDelegate::EconomyHoursAttDelegate(Q Object *parent) :
QItemDelegate(parent)
{
}
/* Reimplemented function: createEditor
* Create a QCheckbox in the att column
*/
QWidget *EconomyHoursAttDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/ ) const
{
QCheckBox *editor = new QCheckBox( parent );
return editor;
}
/* Reimplemented function: setEditorData
* Set the checkbox checked if cell contains a date
*/
void EconomyHoursAttDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QString value = index.model()->data( index, Qt::EditRole ).toString();
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
bool checked = value == "" ? false : true;
checkBox->setChecked( checked );
}
/* Reimplemented function: setModelData
* Set model data to todays date if the checkbox is checked
*/
void EconomyHoursAttDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index ) const
{
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
QDate now = QDate::currentDate();
if( checkBox->isChecked() )
model->setData( index, now, Qt::EditRole );
else
model->setData( index, QString(), Qt::EditRole );
}
/* Reimplemented function: updateEditorGeometry
* Adjust the size of the checkbox to fit the cell size
*/
void EconomyHoursAttDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &/*index*/ ) const
{
editor->setGeometry( option.rect );
}
Any help is apreciated...
//John
I have subclassed a QSqlQueryModel and a QItemDelegate. Making a column containing a date editable, and displayed as a QCheckBox. Cell contains a date = checked cell is null = not checked. Checking a cell = insert todays date, unchecking a cell = set date to "".
The only thing left to do is to allways display the checkbox delegate, even when not editing. After a bit of googling I found out that i have to reimplement the paint() function in my QItemDelegate, to display a CheckBox instead of text.
The problem is that i have no idea where to begin. The manual gives a short example, but it doesent make much sense to me.
Does anyone know of a better example?
The implementation of my QItemDelegate (Basically yanked from the SpinBoxDelegate example):
#include "economyhoursattdelegate.h"
#include <QCheckBox>
#include <QDate>
/* Constructor
*
*/
EconomyHoursAttDelegate::EconomyHoursAttDelegate(Q Object *parent) :
QItemDelegate(parent)
{
}
/* Reimplemented function: createEditor
* Create a QCheckbox in the att column
*/
QWidget *EconomyHoursAttDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/ ) const
{
QCheckBox *editor = new QCheckBox( parent );
return editor;
}
/* Reimplemented function: setEditorData
* Set the checkbox checked if cell contains a date
*/
void EconomyHoursAttDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QString value = index.model()->data( index, Qt::EditRole ).toString();
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
bool checked = value == "" ? false : true;
checkBox->setChecked( checked );
}
/* Reimplemented function: setModelData
* Set model data to todays date if the checkbox is checked
*/
void EconomyHoursAttDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index ) const
{
QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
QDate now = QDate::currentDate();
if( checkBox->isChecked() )
model->setData( index, now, Qt::EditRole );
else
model->setData( index, QString(), Qt::EditRole );
}
/* Reimplemented function: updateEditorGeometry
* Adjust the size of the checkbox to fit the cell size
*/
void EconomyHoursAttDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &/*index*/ ) const
{
editor->setGeometry( option.rect );
}
Any help is apreciated...
//John