Are you able to pastebin a small reproduction of the problem? The example I
posted was from a working bit of code that I wrote.

On Thu, Jun 18, 2015 at 11:12 PM crazygamer <[email protected]> wrote:

>
>
> On Thursday, June 18, 2015 at 1:45:14 AM UTC+5:30, Justin Israel wrote:
>>
>> text()/setText() are convenience calls to data()/setData() with the
>> DisplayRole. So when we reimplement data/setData to handle the DisplayRole,
>> we don't then want to call setText(). We want to completely handle storing
>> our value. So I think you are working against yourself a bit in that logic.
>>
>> But because you said that you don't want to directly work on the items,
>> and instead just perform an action when the button is clicked, this makes
>> it even easier. You don't even need the data/setData approach. All you
>> really need to do is let the QListWidgetItem continue doing its own default
>> behavior, but carry the custom item. You can then loop over all items when
>> the button is clicked and do the operation:
>>
>>
> Gosh I feel dumb. Why didnt I think of it myself. Reminds me to simplify
> ideas while drilling in complex code and getting lost.
>
>
>> http://pastebin.com/UVhNkaMC
>>
>> That example lets you edit the items in the list. When you click the
>> button, it does the rename on the custom item.
>>
>>
> Appreciate the comprehensive reply. Example was more than I asked for. :)
> I understood the example by itself though I am having a bit of a problem
> implementing it in my code.
>
> With the "setItem" method in the CustomItem class, I get this error while
> retrieving the item:
> item = self.ui.loaded_objs_list.item(index)
> No object matches name: <scriptname.CustomItem object at
> 0x0000002AA0534EC8>
>
> class CustomItem(QtGui.QListWidgetItem):
>
>     USER_TYPE = QtGui.QListWidgetItem.UserType + 1
>
>     def __init__(self, customitem, parent=None):
>         super(CustomItem, self).__init__(parent, self.USER_TYPE)
>         self.setItem(customitem)
>
>     def setItem(self, customitem):
>         self.setText(customitem.name().split('|')[-1])
>
> I'm getting a reference to the object instead of the object. I figure its
> a syntax issue but could not fix it.
>
> If I use the earlier method of adding a "customItem" attribute to the
> class, I'm able to retrieve it properly:
> item = self.ui.loaded_objs_list.item(index).customItem
>
> class CustomItem(QtGui.QListWidgetItem):
>
>     USER_TYPE = QtGui.QListWidgetItem.UserType + 1
>
>     def __init__(self, customitem, parent=None):
>         super(CustomItem, self).__init__(parent, self.USER_TYPE)
>         self.customItem = customitem
>         self.setText(customitem.name().split('|')[-1])
>
> I'm adding the objs to the lists like this:
> xx = CustomItem(obj)
> self.ui.loaded_objs_list.addItem(xx)
> xy = CustomItem(obj)
> self.ui.preview_new_names_list.addItem(xy)
>
> ###############################################################
> Marking the above reply as Complete since my tool is working as expected.
> The above question is just for my knowledge.
>
> Thank you Justin for all the help.
> -Harshad
>  ##############################################################
>
>> On Thu, Jun 18, 2015 at 3:21 AM crazygamer <[email protected]> wrote:
>>
>>> Of course I can rename the object directly by doing the rename directly
>>> in setData:
>>>     def setData(self, role, value):
>>>         if role == QtCore.Qt.EditRole:
>>>             self.customItem.rename(value)
>>> Making the Display role also showing correctly as it should.
>>> I'll have to remove the "Rename Objects" button as the rename is
>>> happening directly on edit.
>>>
>>> Now that I have a custom item type in my ui (pymel node), I can do
>>> commands on the node directly(as above). Which I want to avoid if possible.
>>> Till now I have kept the Qt code separate from Maya code. I have one
>>> file with the ui code and another file with maya code. They interact
>>> through the controller using signals. (These methods learnt from Practical
>>> Maya Programming in Python Book) I like the efficiency of keeping them
>>> separate
>>> .
>>> I have a test function in the ui code so I can run the ui on a console
>>> with mayapy and see how it works. Since I made a custom item now, i had to
>>> import pymel in the test function. This takes a few mini-seconds to load.
>>>     def objs_list():
>>>         from pymel.core import createNode
>>>         #objs = []
>>>         objs = [createNode('polyCube'),
>>>                 createNode('polySphere'),
>>>                 createNode('polyCone'),
>>>                 createNode('polyCylinder')]
>>>         return objs
>>>
>>>     def load_objs():
>>>         controller.objList.emit(objs_list())
>>>
>>> I have the tool working acceptable now. These questions are just to know
>>> what is the preferred, efficient or "pythonic" way to write.
>>> Hope you can give me some inputs on how am I doing.
>>> Thanks for taking the time to answer. :)
>>> -Harshad
>>>
>>> --
>>> 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/9bb5346f-9fb7-4f86-afb3-e9ee45d3ac2d%40googlegroups.com
>>> <https://groups.google.com/d/msgid/python_inside_maya/9bb5346f-9fb7-4f86-afb3-e9ee45d3ac2d%40googlegroups.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/30e7ef10-ec61-4bb2-8116-402d8a5c96b2%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/30e7ef10-ec61-4bb2-8116-402d8a5c96b2%40googlegroups.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/CAPGFgA2js%3DJ%2BX2tQyTPZ87a67yJGS_AeDprNCnsV9NNRNrNBMw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to