Sure.

from PySide import QtCore, QtGui

# Some random class
class MyCustomType(object):
    foo = "foo"
    bar = 1
    d = {}
    l = []
    s = set()

# On top of the standard roles that Qt uses such as DisplayRole, 
TextAlignmentRole, ...
# We can add our own data from anything starting with UserRole and higher
# Qt wont touch roles this high up
OBJECT_ROLE = QtCore.Qt.UserRole + 1

tree = QtGui.QTreeWidget()

# Create a random instance
# And a tree item
anObject = MyCustomType()
i = QtGui.QTreeWidgetItem()

# Set our instance as a custom data role
col = 0
i.setData(col, OBJECT_ROLE, anObject)
# add to tree
tree.addTopLevelItem(i)

# Now fetch it back out again by row, column
item = tree.itemAt(0, col)
print item.data(0, OBJECT_ROLE)
# <__main__.MyCustomType at 0x103f1d090>




On Sep 24, 2013, at 8:38 PM, shapeofmotion wrote:

> On Sunday, September 22, 2013 10:02:07 PM UTC+2, Justin Israel wrote:
>> Why does the object need to be serialized to json? Can't you just store your 
>> object as data directly?
>> 
>> Also, you could subclass QTreeWidgetItem and store a custom item in the 
>> tree. 
>> 
>> On Sep 22, 2013 11:39 PM, "shapeofmotion" <[email protected]> wrote:
>> 
>> Hi,
>> 
>> First off thanks for a great forum!
>> 
>> 
>> 
>> I am using the pyside QTreeWidget to create a basic ui to list which 
>> materials and or SG that has a specific V-Ray material ID assigned to it. 
>> The widget lists parent items with the shader ID as label and the child 
>> items are the materials or SG that have that ID assigned.
>> 
>> 
>> 
>> 
>> I have assigned the name of the material to the data attr so that when the 
>> item is clicked the material gets selected in the hypershade. As it works 
>> now if I were to rename the material it would no longer work since the 
>> widget is pointing to the name and not to the actual object.
>> 
>> 
>> 
>> 
>> I am sort of stuck here, my initial idea was to serialize a dict that was 
>> pointing to the pyNode, but the JSON.dumps method is giving me errors that 
>> my dict is not JSON serializable.
>> 
>> 
>> 
>> I am pretty new to pyside and especially the qtreewidget so any tips, ideas 
>> or pointers are very welcome!
>> 
>> 
>> 
>> Thanks, Johan
>> 
>> 
>> 
>> --
>> 
>> 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 post to this group, send email to [email protected].
>> 
>> For more options, visit https://groups.google.com/groups/opt_out.
> 
> As you pointed out the best way is obviously to store the object as a custom 
> data type in the threewidgetitem. I am quite new to pyside and found the 
> documentation (http://srinikom.github.io/pyside-docs/) on the subject a bit 
> hard to grasp. Do you have any good tips on how to do this or can you point 
> me to some information on the subject?
> 
> Best regards,
> Johan
> 
> -- 
> 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 post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
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 post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to