Well,

Not that I have figured out how to fix my code, but I did find out the 
documentation that explains that in *great* details (and in a rather clear 
way). It's there for those who are still looking for :

pyside/shiboken/doc/ownership.rst

I'm a little bit ashamed of not finding this earlier

stF


On Mon, Jan 09, 2012 at 10:36:32PM +0100, S. Champailler wrote:
> Dear mailing list,
>
> In my python code, I usually create QStandardItem with some data, but now I 
> realize it may be plain wrong :
>
>
> def make_simple_model(my_data)
>    model = QtGui.QStandardItemModel(1, 1)
>
>    item = QtGui.QStandardItem()
>    item.setData(my_data, Qt.UserRole)
>    model.setItem(0, 0, item) # Model takes ownership of the Item (if I 
> understand Qt's doc well)
>
>    return model
>
> my_data = ... # some kind of Python object
> model = make_simple_model(my_data)
>
> # After this, Python has lost track of my data
> my_data = None
>
> # This one is correct, because model is still tracked by Python and the item 
> is owned by the model
> item = model.item(0,0)
>
> # But this is dangerous (because the data has been lost)
> item.data( Qt.UserRole)
>
>
>
> If I got the "PySide pitfalls" page well, this code is wrong : my_data got 
> lost (last line of the code) and therefore, the QStandardItem UserRole data 
> is a dangling p\
> ointer...
>
> Is my interpretation correct ? If so, I'd be happy to add this to the 
> "pitfalls" page. Although that page does explain the issue, somehow, I 
> thought this code was cor\
> rect until now (it worked thousands of times) => maybe other developpers may 
> have the same misunderstanding.
>
> Thx,
>
> Stefan
>
>
>
> _______________________________________________
> PySide mailing list
> [email protected]
> http://lists.pyside.org/listinfo/pyside
_______________________________________________
PySide mailing list
[email protected]
http://lists.pyside.org/listinfo/pyside

Reply via email to