<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Hi Jan!</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">When working with the QAbstractItemModel, items can only be deleted via<br>

resetting the model, or when removing rows/columns. The row/column removal<br>
shall happen in three phases:<br>
<br>
1) call beginRemoveRows<br>
2) remove the internal representation, freeing the MyObject instances<br>
3) call endRemoveRows<br>
<br>
I will argue that if a delegate is alive after the beginRemoveRows<br>
finished, then the QML view is broken. The view shall react to<br>
rowsAboutToBeRemoved and kill any delegates in response to that signal.<br>
<br>
However, you absolutely must not free your internal representation prior to<br>
calling beginRemoveRows. Are you sure that this is not the case?<br>
<div class="im"><br></div></blockquote><div><br></div><div>Yes. When deleting, I basically do this:</div><div><br></div><div>(m_objs_ is a QList<MyObject*> )</div><div>int rm_idx; // the index to remove</div><div><br>
</div><div>m_objs_[rm_idx]->deleteLater();</div><div>beginRemoveRows(QModelIndex(), rm_idx, rm_idx);<br></div><div>m_objs_.removeAt(rm_idx);<br></div><div>endRemoveRows();</div><div><br></div><div>I verified that the object is deleted "later". </div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
> Item {<br>
>    id: theDelegate<br>
>    property string someValue: myObject ? myObject.someObjectValue : ""<br>
> }<br>
><br>
> but this is just ugly.<br>
<br>
</div>Something is apparently picking up some signal which causes the delegate to<br>
notice that the value of "myObject" has changed. What signal is it? Are the<br>
delegates alive even after rowsAboutToBeRmoved is emitted?<br>
<div class="im"><br></div></blockquote><div><br></div><div>The delegate is alive for a short period after this, it seems. I guess maybe just another event loop cycle? I didn't investigate it further yet though.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><span style="color:rgb(34,34,34)">It's interesting that you've solved the problem of too many roles by using</span><br>
</div>
a QObject instance within the model itself. I wonder whether this is<br>
actually a good path -- QObjects are rather heavyweight, so it is a<br>
question whether you gain anything by trading the disadvantage of "got to<br>
call the model's data() too many times, once for each role" by another<br>
problem, "got to manage expensive QObjects instead of some liteweight<br>
internal nodes".<br>
<br>
Perhaps it fits the rest of the code and using QObjects "inside" the model<br>
makes sense. However, if that was not the case, simply emitting<br>
dataChanged() when any "property" changes migth be even easier (and would<br>
elliminate the need to use properties and separate signals in the first<br>
place).<br>
<br></blockquote><div><br></div><div>The "MyObject" class is a QObject for various reasons, I have some other objects of the same type that aren't stored in this list. So it looked like a decent solution. But I guess it's a bit outside the normal use of the model/views. </div>
<div><br></div><div>I don't instance that many of them (5-10) so the performance seems decent enough.</div><div><br></div><div>Thanks for the feedback,</div><div>Ola</div><div><br></div><div><br></div><div><br></div><div>
<br></div><div><br></div></div></div></div>