setItemWidget causing crash

2020-07-18 04:18发布

问题:

I'm trying to add a QLabel to a QTreeWidgetItem but Python crashes on the setItemWidget call.

Any ideas why it crashes?

Here is the code:

from PySide import QtCore, QtGui

view = QtGui.QTreeWidget()
view.show()

newItem =  QtGui.QTreeWidgetItem(view)
view.setItemWidget(newItem,0,QtGui.QLabel('abc'))

回答1:

You need to keep a reference to the label, or give it a parent:

    view.setItemWidget(newItem, 0, QtGui.QLabel('abc', view))