PDA

View Full Version : setModelData for subclassed QItemDelegate



T4ng10r
25th May 2007, 18:30
In setModelData for my delegate I perform this code



void ShipComponentDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QString name = index.model()->data(index, Qt::DisplayRole).toString();
QSize size(0,0);
QVariant var = QVariant(size);
model->setData(index, var,Qt::SizeHintRole);
size = model->data(index, Qt::SizeHintRole).toSize();
}

size taken from model after setting should be the same as above. But it isn't. I thought first time that I made some error with values inserted into size and it is invalid. But no - in every other case with all other values - it doesn't want to change internal data.
To clarify all - in setEditorData() I fill this index with my size - then after changes I want to bring this size to previous value.

wysota
25th May 2007, 20:05
I suggest you set an empty QVariant as the value.

T4ng10r
25th May 2007, 20:39
Tried - doesn't work.
It's like model doesn't want to fill with new value. But it has to change, because this value is needed somewhere else in delegate.
I've checked internal setData code - it isn't setting only when index is invalid or data is invalid. But index.isValid() returns TRUE and QVariant which is being set - also returns true for validity test.

marcel
25th May 2007, 20:47
What model are you using?

T4ng10r
25th May 2007, 20:57
QStandardItemModel. In setModelData it's downcasted to QAbstractItemModel. Only one think interest me - whole method is const. But - if I remeber correctly - const correctness is being tested only during compilation time.
SetData is virtual function - so even when I get QAbstractItemModel pointer to QStandardItemModel - then setData should use method from QStandardItemModel.

During setData processed by QStandardItemModel it calls setData for QStandardItem which uses QVector<QWidgetItemData> values - there should be no time delays.
The only think that bothers me is signal sent after data set - maybe there's a problem.

marcel
25th May 2007, 21:08
No, I thought you were using a custom model and had setData broken.

What about this? ( from QStandardItem::setData )


QVariant oldValue = data(role);
if (value == oldValue)
return;


As you can see no value is set if it is equal to the old one.

What values are in the model?
You are always setting QSize(0,0) and get it back.


(I'm using Qt 4.2.2 ).

Regards

T4ng10r
25th May 2007, 21:49
Good point - this condition return no error value.
But - when I setData to QVariant(QSize(10,10)), then to QVariant(QSize(20,20)) and then to QVariant(QSize(30,30)) - one line after another - it still returns only first data, set at the begin.

T4ng10r
27th May 2007, 12:09
Situation has been clarified. Each time when setData for model used in view is being used - by inside signal setEditorData for delegate is being called.
So when I was setting SizeHintRole in setEditorData to editor size - then when in setModelData I was clearing this role - model was calling setEditorData and all was set to the previous state.
When I moved setting SizeHintRole to createEditor - all problems were gone.