PDA

View Full Version : QTableView and QStyledItemDelegate ownership



lxman
7th July 2011, 01:02
I have a QTableView under which I am showing several different QSqlTableModel's (in succession, of course, not all at once). I am using a host of delegates to get the views the way that I want them.

I am presuming, as setItemDelegateForColumn() is a member of QTableView, not QSqlTableModel, that once I set a delegate, then change models, my delegate is still with the view, and it has to be cleared out/deleted/whatever to line up with the new model.

Am I correct in thinking this, or do the delegates actually stay with the model?

ChrisW67
7th July 2011, 04:50
Delegates are view related not model related. A quick experiment/code inspection confirms that delegates stay set despite changing the viewed model. Shrinking the column count even leaves delegates on the higher column numbers alone until the column count increases again.

If you do change them be aware that:

Any existing delegate will be removed, but not deleted. QAbstractItemView does not take ownership of delegate.

Santosh Reddy
7th July 2011, 05:19
Am I correct in thinking this, or do the delegates actually stay with the model?
Yes, delegate remains with view, If you look at the dependencies,

- View depends on Model
- View depends on Delegate
- Delegate depends on Model
- Model in independent of View and Delegate
- Delegate in independent of View

So when you change the Model for a given View (say from Model1 to Model2), then you should be also the changing the Delegate for that corresponding Model.