Hey David,

I wanted to add that you should not use the internalPointer method outside
of the internal code of your model. Normally in Qt land that would return a
pointer to something outside components shouldn't be messing with but I
know it feels natural to use it when you are setting your own objects.
Probably better to write an abstraction around that as a method on your
model.
What I tend to do is create custom role types and serve stuff through the
data()  method.

Something like:

# class attribute on model
ObjectRole = QtCore.Qt.UserRole+1
...
def data(index, role):
    if role == self.ObjectRole:
        # resolve and return the object
   ....

That way you are covered if your model stores it's objects in a private
list or dict or internalPointer.

Something to keep an eye out for... I just recently had to fix a tricky bug
where randomly my model would crash saying that various attributes didn't
exist on a custom item I store in my model,  when they are part of the
subclass implementation. Turned out that using internalPointer as the only
place to store your object can lead to garbage collection issues and
corruption. It would return broken instances of my items. I found similar
reports of this online. Solution was to store my own mapping of the objects
as well and manage adding and removing them properly, and then storing only
a unique id as the internalPointer to look them up.
Thus was only one specific model that had this problem, as other models in
my app were purely using internal lists and no internalPointer calls.

Also Erkan,  I think your answer was meant for a QTreeWidget where you are
dealing with converting between indexes and model items.
 On Jun 14, 2014 12:57 AM, "Marcus Ottosson" <[email protected]> wrote:

> It's my pleasure, Erkan. I'm glad you like it.
>
>
> On 13 June 2014 13:18, Erkan Özgür Yılmaz <[email protected]> wrote:
>
>> Thanks a lot for sharing this, it is a great source and a great example
>> to the tons of things that I've to learn from you guys.
>>
>> Ozgur
>> eoyilmaz.blogspot.com
>> On Jun 13, 2014 3:11 PM, "Marcus Ottosson" <[email protected]>
>> wrote:
>>
>>> To run it with PyQt4/PySide, you can import QtGui instead of QtWidgets,
>>> and do a string-replace of all occurrences. There isn't anything unique to
>>> PyQt5 in these examples, they should be backwards compatible. (if not, let
>>> me know!. And finally, PySide has the Signal class instead of the
>>> pyqtSignal of PyQt.
>>>
>>> Best,
>>> Marcus
>>>
>>>
>>> On 13 June 2014 13:08, David Martinez <[email protected]>
>>> wrote:
>>>
>>>> Thanks a lot Marcus! I will definitely take a look.    :-)
>>>>
>>>>
>>>> --
>>>> David Martinez - Technical Animator
>>>>
>>>> Email: [email protected]
>>>> Website: http://www.elusiveideas.com
>>>>
>>>>
>>>>
>>>>
>>>> On Fri, Jun 13, 2014 at 1:05 PM, Marcus Ottosson <
>>>> [email protected]> wrote:
>>>>
>>>>> If you're interested in MVC in general, I just finished up on an
>>>>> example of implementing MVC in Python, using Qt and it's QEvent and
>>>>> pyqtSignal frameworks, a custom Controller and two custom Views.
>>>>>
>>>>> Maybe it could be useful in your exploration. :)
>>>>>
>>>>> https://github.com/abstractfactory/labs/tree/master/python/mvc
>>>>>
>>>>> Best,
>>>>> Marcus
>>>>>
>>>>>
>>>>> On 13 June 2014 12:51, Marcus Ottosson <[email protected]> wrote:
>>>>>
>>>>>> Glad to hear you got it working. :)
>>>>>>
>>>>>>
>>>>>> On 13 June 2014 12:11, David Martinez <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> It's working now! Thanks a lot!
>>>>>>>
>>>>>>> Since I'm going through the proxy model, I had to use the
>>>>>>> 'mapToSource' method instead of the itemFromIndex.
>>>>>>> The final code looks like this:
>>>>>>>
>>>>>>>         global_position = self.uiTree.mapToGlobal(pos)
>>>>>>>         index = self.uiTree.indexAt(pos)
>>>>>>>         proxyModel = self.uiTree.model()
>>>>>>>         item = proxyModel.mapToSource(index)
>>>>>>>
>>>>>>>         node = item.internalPointer()
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> David Martinez - Technical Animator
>>>>>>>
>>>>>>> Email: [email protected]
>>>>>>> Website: http://www.elusiveideas.com
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Fri, Jun 13, 2014 at 11:57 AM, Marcus Ottosson <
>>>>>>> [email protected]> wrote:
>>>>>>>
>>>>>>>> Looks like Erkan beat me to it. :)
>>>>>>>>
>>>>>>>>
>>>>>>>> On 13 June 2014 11:57, Marcus Ottosson <[email protected]>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> The item you're getting is indeed from the proxy model. You'll
>>>>>>>>> have to get it, and then map it to the original.
>>>>>>>>>
>>>>>>>>> http://qt-project.org/doc/qt-4.8/qabstractproxymodel.html#mapToSource
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On 13 June 2014 11:40, David Martinez <
>>>>>>>>> [email protected]> wrote:
>>>>>>>>>
>>>>>>>>>> Hi,
>>>>>>>>>>
>>>>>>>>>> I've been experimenting with Model View Programming and so far,
>>>>>>>>>> I've been able to create a QTreeView which displays the data of my 
>>>>>>>>>> custom
>>>>>>>>>> model (Inheriting from 'QtCore.QAbstractItemModel'). I created my own
>>>>>>>>>> custom kind of object to use within the model (as I'm storing some 
>>>>>>>>>> data
>>>>>>>>>> about the items being stored). I use the 'internalPointer' method in 
>>>>>>>>>> the
>>>>>>>>>> model to get a reference to my original object if I need it. I also 
>>>>>>>>>> have a
>>>>>>>>>> 'QtGui.QSortFilterProxyModel' between the view and the model which 
>>>>>>>>>> allows
>>>>>>>>>> me to filter and sort the results.
>>>>>>>>>>
>>>>>>>>>> Everything seems to be working as expected but I'm struggling to
>>>>>>>>>> get something working. I want to be able to right click an item on 
>>>>>>>>>> the
>>>>>>>>>> treeView and get a popup menu which contains information about the 
>>>>>>>>>> specific
>>>>>>>>>> item.
>>>>>>>>>>
>>>>>>>>>> That means that I'm interested on the 'QModelI' value in the
>>>>>>>>>> 'Model' for the element selected in the 'QTreeView' so I can 
>>>>>>>>>> retrieve the
>>>>>>>>>> original node using 'internalPointer' and get the information that I 
>>>>>>>>>> need.
>>>>>>>>>>
>>>>>>>>>> I'm not sure how to get the index in the original 'model' though.
>>>>>>>>>> I have tried to use the 'indexAt(QPos)' method of the treeView but I 
>>>>>>>>>> guess
>>>>>>>>>> that this is returning the index that it has within the view and not 
>>>>>>>>>> the
>>>>>>>>>> model.
>>>>>>>>>>
>>>>>>>>>> I'm not sure if I would need something like a 'selectionModel'. I
>>>>>>>>>> started to try that but I think that it would give me the index of 
>>>>>>>>>> the
>>>>>>>>>> 'QSortFilterProxyMode' instead of the one of 'QAbstractItemModel'.
>>>>>>>>>>
>>>>>>>>>> Is there anything obvious that I'm missing?
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Thanks in advance,
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>> David Martinez - Technical Animator
>>>>>>>>>>
>>>>>>>>>> Email: [email protected]
>>>>>>>>>> Website: http://www.elusiveideas.com
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>  --
>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>> Google Groups "Python Programming for Autodesk Maya" group.
>>>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>>>> send an email to [email protected].
>>>>>>>>>> To view this discussion on the web visit
>>>>>>>>>> https://groups.google.com/d/msgid/python_inside_maya/CAMLeNpwXVJS7Ko5_XUf3cTRML%2BpXBjt8h1Pm2EXz03HQaobR3Q%40mail.gmail.com
>>>>>>>>>> <https://groups.google.com/d/msgid/python_inside_maya/CAMLeNpwXVJS7Ko5_XUf3cTRML%2BpXBjt8h1Pm2EXz03HQaobR3Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>>>>>> .
>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>> *Marcus Ottosson*
>>>>>>>>> [email protected]
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> *Marcus Ottosson*
>>>>>>>> [email protected]
>>>>>>>>
>>>>>>>> --
>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>> Groups "Python Programming for Autodesk Maya" group.
>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>> send an email to [email protected].
>>>>>>>>  To view this discussion on the web visit
>>>>>>>> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBL-ctm%3DZORqzDk%2B0L4tJmfDr_D7yQ4E-U%3DWVdePWMGqA%40mail.gmail.com
>>>>>>>> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBL-ctm%3DZORqzDk%2B0L4tJmfDr_D7yQ4E-U%3DWVdePWMGqA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>>>> .
>>>>>>>>
>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>
>>>>>>>
>>>>>>>  --
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "Python Programming for Autodesk Maya" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>> send an email to [email protected].
>>>>>>>  To view this discussion on the web visit
>>>>>>> https://groups.google.com/d/msgid/python_inside_maya/CAMLeNpy7kocGrZXRSNhYfqUASVTwnHzm6FZmAQK4TLpUyrL66g%40mail.gmail.com
>>>>>>> <https://groups.google.com/d/msgid/python_inside_maya/CAMLeNpy7kocGrZXRSNhYfqUASVTwnHzm6FZmAQK4TLpUyrL66g%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>>
>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> *Marcus Ottosson*
>>>>>> [email protected]
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> *Marcus Ottosson*
>>>>> [email protected]
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Python Programming for Autodesk Maya" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>>  To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCnUwWP3oSHxPQiWcOVH0e40iJxU%3D5dpSK%2BNWzWrG2L%2BQ%40mail.gmail.com
>>>>> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCnUwWP3oSHxPQiWcOVH0e40iJxU%3D5dpSK%2BNWzWrG2L%2BQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Python Programming for Autodesk Maya" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>>  To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/python_inside_maya/CAMLeNpwCw1k5xqx1cMY_bitayYF74DJr%3D-%2BarDebKriq3Mg6ZQ%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/python_inside_maya/CAMLeNpwCw1k5xqx1cMY_bitayYF74DJr%3D-%2BarDebKriq3Mg6ZQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>>
>>>
>>> --
>>> *Marcus Ottosson*
>>> [email protected]
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Python Programming for Autodesk Maya" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>  To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCz3jDmqvV4kGYJ01XCFxVyA-edh-3ibT8rGWxNPAB%2BQA%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCz3jDmqvV4kGYJ01XCFxVyA-edh-3ibT8rGWxNPAB%2BQA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>>  To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CAGNmyx7tt8rDLDEQUEEYR2dBC0-RwpDd0JeUiCxJufHEkJv8Cg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/python_inside_maya/CAGNmyx7tt8rDLDEQUEEYR2dBC0-RwpDd0JeUiCxJufHEkJv8Cg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> *Marcus Ottosson*
> [email protected]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCH1aLNoYNh%2BTkd4EYu6RVg6v9r6Sc1%3DhsP-K_AGeiuZQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCH1aLNoYNh%2BTkd4EYu6RVg6v9r6Sc1%3DhsP-K_AGeiuZQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2FQQQYYLZoZu9KK_A0yr%2BVdQmt9qpv-c9QhT3sHZHDog%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to