Help needed with QItemDelegate
Hi,
I'm busy with my QItemDelegate and my custom model!
But there are a few things that I don't get quite well.
Correct me if I'm wrong!
The painter function of the QItemDelegate is invoked when the view sees the delegated cell.
The createEditor function is invoked when the user wants to edit the cell.
The setEditorData function is invoked after the widget is created.
And the setModelData is invoked when the user finished editing.
So with this knowledge in the back of my head I started coding!
Code:
#include "checkBoxDelegate.h"
#include <QtGui>
CheckBoxDelegate
::CheckBoxDelegate(QObject *parent
){
qDebug("CheckBoxDelegate::CheckBoxDelegate(): constructing delegate");
}
{
qDebug("CheckBoxDelegate::paint(): painting the delegate");
if(!model)
if(index.data() == 2)
{
}else{
}
}
{
qDebug("CheckBoxDelegate::createEditor(): create checkbox");
if(!model)
if(QVariant(model
->data
(index, Qt
::EditRole)).
toInt() == 2) checkbox->setCheckState(Qt::Checked);
else
checkbox->setCheckState(Qt::Unchecked);
return checkbox;
}
{
qDebug("CheckBoxDelegate::setEditorData(): set editor data");
QCheckBox *checkbox
= qobject_cast<QCheckBox
*>
(editor
);
if(!checkbox)
if(QVariant(model
->data
(index, Qt
::EditRole)).
toInt() == 2) checkbox->setCheckState(Qt::Checked);
else
checkbox->setCheckState(Qt::Unchecked);
}
{
qDebug("CheckBoxDelegate::setModelData(): Set model data");
if(!index.isValid())
return;
}
The idea is to show a checkbox with a green or red background. Indicating whether it's checked or not. (It's a little bit strange but it has to be a field that catches your attention).
And the background drawing works, its green when the state is 2 and red when it's 0.
But I can't see the widget until I start editing the cell.
Is there a way that the checkbox is all ready visible before I start editing.
I have the same problem with the date delegate, it isn't visible when the table is loaded!
Greetings
Cyberboy
Re: Help needed with QItemDelegate
The editor is created only when a particular cell needs editing, so in normal conditions at most one editor per view exists. You can make a persistant editor, but doing that for more than a few items slows down the application terribly.
Now the good news - you don't need a custom delegate :) The standard delegate can draw the checkbox for you if you use the Qt::CheckStateRole and return Qt::ItemIsUserCheckable among the flags for a particular item.
Re: Help needed with QItemDelegate
Oke, so I have to pass a Qt::CheckStateRole in my model for the particular column/cell and the standard delegate will take care of the rest?
(There isn't a standard for a QDateEdit, is it? At least I didn't found one in the docs!)
Re: Help needed with QItemDelegate
Date time edit has to be handled using a real editor, but you can emulate its looks using a custom delegate.