[PySide] Parenting a QWidget to a QTreeWidgetItem crashes

Skip to first unread message


Mar 15, 2018, 6:49:23 PM3/15/18
to Python Programming for Autodesk Maya

I'm trying to add an icon to a QTreeWidget, and maya doesn't seem to like the way I do it.. I've got two versions below, the first one is working fine(ish), the second makes maya crash, and I'd like to understand why !

from PySide.QtGui import *
# working version
tree = QTreeWidget()
itm = QTreeWidgetItem(tree, ['item1'])
# widget creation
w = QWidget()
lyt = QHBoxLayout(w)
lbl = QLabel('---')
itm.setSizeHint(1, w.sizeHint())
tree.setItemWidget(itm, 1, w)

class QNode(QTreeWidgetItem):
def __init__(self, parent, name_list):
super(QNode, self).__init__(parent, name_list)
def attach_icon(self):
# widget creation
icons_widget = QWidget()
lyt = QHBoxLayout(icons_widget)
start_lbl = QLabel('---')
self.setSizeHint(1, icons_widget.sizeHint())
self.treeWidget().setItemWidget(self, 1, icons_widget)
tree = QTreeWidget()
itm = QNode(tree, ['item1'])

I'm running out of ideas, so any suggestion or hint you have is more than welcome =]
Thank you !


Mar 16, 2018, 7:48:06 AM3/16/18
to Python Programming for Autodesk Maya
there is something really magic with this forum... I try not to post unless I'm really stuck, and running out of ideas. But somehow, everytime I post here, I find a solution to my problem within the day(s)
So again, a few hours after I created this topic, I found the solution ^^ If someone else has the same issue : I just had to give a parent to my label if I create it in the class !
So problem solved =] thank you !

Marcus Ottosson

Apr 7, 2021, 11:57:28 AMApr 7
to python_in...@googlegroups.com

If I had any hair, I would have been pulling it out if it wasn’t for this post. Thank you from the future! :D The minor difference was that I had to parent my QTreeWidgetItem to the QTreeWidget, and each child item to its parent item. Something I had assumed happened automatically when you add an item to another, similar to how QHBoxLayout takes ownership of widgets when they are added. But, apparently not. Problem solved!

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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/f0d85165-3018-4296-930a-61f1e42602f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vince touache

Apr 9, 2021, 4:36:22 PMApr 9
to Python Programming for Autodesk Maya
hahaha awesome, thanks for sharing =]
Reply all
Reply to author
0 new messages