[PySide] Parenting a QWidget to a QTreeWidgetItem crashes

146 views
Skip to first unread message

fruit...@gmail.com

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

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()
tree.show()
tree.setColumnCount(2)
itm = QTreeWidgetItem(tree, ['item1'])
# widget creation
w = QWidget()
lyt = QHBoxLayout(w)
lbl = QLabel('---')
lyt.addWidget(lbl)
itm.setSizeHint(1, w.sizeHint())
tree.setItemWidget(itm, 1, w)

# OPTION B
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('---')
lyt.addWidget(start_lbl)
self.setSizeHint(1, icons_widget.sizeHint())
self.treeWidget().setItemWidget(self, 1, icons_widget)
tree = QTreeWidget()
tree.show()
tree.setColumnCount(2)
itm = QNode(tree, ['item1'])
itm.attach_icon()

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

fruit...@gmail.com

unread,
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

unread,
Apr 7, 2021, 11:57:28 AM4/7/21
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

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