PDA

View Full Version : Subclass QStandardItem



un
14th August 2015, 10:52
Hello,

I know there have been similar questions, but I did not find my specific problem. Well here is what I did:

- subclassed QTreeView
- subclassed QStandardItem: implemented clone() data() setData() type()
- subclassed QStandardItemModel
- subclassed QItemDelegate

Now, what I wanted is that the delegate does not operate directly on the text the users sees (Qt::DisplayRole) but on the edit role (Qt::EditRole). I expected that when I set QStandardItemModel::setItemPrototype to my subclassed QStandardItem it will call the data() and setData() functions of the subclass.
Well, this does not seem to be the case. It still uses the base class data() and setData() functions. Only when i also overwrote data() and setData() for my subclass of QStandardItemModel, then the behaviour was as expected.

So, why is it necessary to call setItemPrototype() at all, when I have to overwrite data() and setData() for my subclass of QStandardItemModel anyways (and there I can use the correct type)? Also I found this by try-and-error, I don't think this is documented anywhere.

Maybe someone has tried this before?

anda_skoa
14th August 2015, 11:06
Did you add instances of your QStandardItem subclass when populating the model?

Cheers,
_

un
14th August 2015, 13:11
Did you add instances of your QStandardItem subclass when populating the model?

Cheers,
_

Yes. As explained, it seems to work after I implemented data() and setData() in the subclass of QStandardItemModel. I expected that setting the prototype with "setItemPrototype()" is enough, but it seems it is not and I don't know why.

anda_skoa
14th August 2015, 14:54
Well, setItemPrototype() would have no affect if you added QStandardItem instances to your model, so I wanted to verify that you hadn't accidentally done that.

I assume you have also checked that the signatures of the overloaded methods match exactly?

Cheers,
_

un
14th August 2015, 16:09
Well, setItemPrototype() would have no affect if you added QStandardItem instances to your model, so I wanted to verify that you hadn't accidentally done that.

I assume you have also checked that the signatures of the overloaded methods match exactly?

Cheers,
_

Yes, they match.

Edit: The problem was solved. It seems my project was not recompiled, after clean / build it works as expected. So everything is correct.