In my project, I subclassed QStyledItemDelegate
and returned a custom editor from the createEditor
function.
QWidget* TagEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
TagEditWidget* tagEditWidget = new TagEditWidget(parent, index.data(Qt::UserRole+4).toInt(), index.data(Qt::UserRole+2).toByteArray(), index.data(Qt::UserRole+3).toByteArray(), index.parent().data(Qt::UserRole+4).toInt() == 9, parent->width());
return tagEditWidget; //tagEditWidget is my custom QWidget
}
When the editing finishes, I want to write the new data back to the model. So I overrode setModelData
.
void TagEditDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
TagEditWidget * tagEditWidget = qobject_cast<TagEditWidget*>(editor);
if (!tagEditWidget)
{
QStyledItemDelegate::setModelData(editor, model, index);
return;
}
//Edit model here?
}
This works, but the problem is that setModelData
gets called no matter HOW the editor was closed. I only want to write the new data if the editor closed using the EndEditHint
, QAbstractItemDelegate::SubmitModelCache
. So I connected the closeEditor
signal to a slot I made called editFinished
.
connect(this, SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)), this, SLOT(editFinished(QWidget*,QAbstractItemDelegate::EndEditHint)));
So now I am able to see HOW the editor closed via the EndEditHint
and if I should write the data back to the model. Buuuuut, setModelData
get's called BEFORE the closeEditor
signal. How does one write the data back to the model when the closeEditor
signal gets called last? Am I missing something here?