Thanks again Justin, that makes sense I've tweaked the code so the data method returns a reference to the object when using a custom role :-)
-- David Martinez - Technical Animator Email: [email protected] Website: http://www.elusiveideas.com On Mon, Jun 16, 2014 at 2:39 PM, Justin Israel <[email protected]> wrote: > No you are right. The QtCore.Qt.UserRole is just the start of the custom > value roles that you are allowed to use in your model, which guarantee not > to conflict with Qt's own roles. So you can do stuff like this: > > ObjectRole = QtCore.Qt.UserRole > IdRole = QtCore.Qt.UserRole+1 > ColorRole = QtCore.Qt.UserRole+2 > FooRole = QtCore.Qt.UserRole+3 > > And then you just pass those around when you call the data() method or in > any function in the model/view that tasks an item role as a parameter. They > are just numbers that you use as constants. And you can return anything you > want, be it the entire object, or parts of an object for convenience. It is > just a way for the model to know how to exactly get a desired value. An > example of a useful reason for returning more specific data from your > custom roles might be QSortFilterProxy, where it has methods for telling it > which roles to use for filtering and sorting: > > http://qt-project.org/doc/qt-4.8/qsortfilterproxymodel.html#filterRole-prop > http://qt-project.org/doc/qt-4.8/qsortfilterproxymodel.html#sortRole-prop > > In this case, it is code that is within QSortFilterProxy, and you are just > telling it where to get custom data to drive the class. > This is part of the interface design that allows views and models to > generically be linked up. > > > > > On Mon, Jun 16, 2014 at 4:31 AM, David Martinez < > [email protected]> wrote: > >> Would it be OK to have a reference to 'internalPointer' within the model >> and return the result when the data method is called with the custom role? >> >> I hope that I'm not missing anything obvious >> >> >> Dave >> >> >> -- >> David Martinez - Technical Animator >> >> Email: [email protected] >> Website: http://www.elusiveideas.com >> >> >> >> >> On Mon, Jun 16, 2014 at 10:25 AM, David Martinez < >> [email protected]> wrote: >> >>> Looking at your example, I have a couple of questions: >>> >>> >>> - Are you using the role to return a reference to your object or >>> returning the data that you were trying to retrieve from it? If you are >>> doing the second thing, are you creating a series of user roles that >>> return >>> a few different values from the model? Is there a good reason to do one >>> thing or the other? >>> >>> - Also, could you explain what do you mean by 'resolving' the object? >>> >>> >>> The way I see it, returning a reference to the object would allow me to >>> do as many operations as I need with it without having to call the data >>> method on the model each time. >>> >>> Thanks in advance >>> >>> >>> -- >>> David Martinez - Technical Animator >>> >>> Email: [email protected] >>> Website: http://www.elusiveideas.com >>> >>> >>> >>> >>> On Mon, Jun 16, 2014 at 10:08 AM, David Martinez < >>> [email protected]> wrote: >>> >>>> That makes sense! Thanks Justin! >>>> >>>> I will do the appropriate changes on my code so it does not use >>>> internalPointer anymore. I'll take it that 'QtCore.Qt.UserRole' returns the >>>> first index for custom roles and that I can create as many of them as I >>>> need, right? >>>> >>>> >>>> >>>> -- >>>> David Martinez - Technical Animator >>>> >>>> Email: [email protected] >>>> Website: http://www.elusiveideas.com >>>> >>>> >>>> >>>> >>>> On Fri, Jun 13, 2014 at 9:20 PM, Justin Israel <[email protected]> >>>> wrote: >>>> >>>>> >>>>> On Jun 14, 2014 8:17 AM, "Justin Israel" <[email protected]> >>>>> wrote: >>>>> > >>>>> > Also Erkan, I think your answer was meant for a QTreeWidget where >>>>> you are dealing with converting between indexes and model items. >>>>> > >>>>> >>>>> Correction. I should have said a QTreeView in combination with >>>>> QStandardItemModel subclasses where it deals with items in the model. >>>>> QAbstractItemModel is index like the view. >>>>> >>>>> > 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 >>>>> . >>>>> >>>>>>>>>>> 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 >>>>> . >>>>> >>>>>>>>> >>>>> >>>>>>>>> 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 >>>>> . >>>>> >>>>>>>> >>>>> >>>>>>>> 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 >>>>> . >>>>> >>>>>> >>>>> >>>>>> 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 >>>>> . >>>>> >>>>> >>>>> >>>>> 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 >>>>> . >>>>> >>>> >>>>> >>>> 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 >>>>> . >>>>> >>> >>>>> >>> 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 >>>>> . >>>>> >> 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/CAPGFgA0dNpOefm%3DLppQNrKrHPki-yOxifFM30jmKeq34NVn59Q%40mail.gmail.com >>>>> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0dNpOefm%3DLppQNrKrHPki-yOxifFM30jmKeq34NVn59Q%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/CAMLeNpwpDT_mKd-3R1yzBhHeckvrY9DDdwq4-DJGXcvPto0kMw%40mail.gmail.com >> <https://groups.google.com/d/msgid/python_inside_maya/CAMLeNpwpDT_mKd-3R1yzBhHeckvrY9DDdwq4-DJGXcvPto0kMw%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/CAPGFgA0B0x6CiLFR0XFr_UmkmdanSGA2Wcy_Z0iRirAiC70rfw%40mail.gmail.com > <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0B0x6CiLFR0XFr_UmkmdanSGA2Wcy_Z0iRirAiC70rfw%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/CAMLeNpwG%3DPKDsaMQWnfgmBhjMb1tGLniG2KXuL1%3DJJjF18gOUQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
