[PyQt] QTreeWidget setItemWidget dissapears after drag & drop

Phil Thompson phil at riverbankcomputing.com
Fri Mar 5 12:31:58 GMT 2010


On Fri, 5 Mar 2010 16:09:42 +1300, Taylor Carrasco <crackerbunny at gmail.com>
wrote:
> I'm trying to keep a widget put into a QTreeWidgetItem after a reparent
> (drag and drop) using QTreeWidget.setItemWidget()
> 
> But the result, if you compile the following code - is that the widget
> inside the QTreeWidgetItem disappears. Any idea why? What code would fix
> this (repopulate the QTreeWidgetItem with the widget again?)
> 
> 
> 
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> 
> class InlineEditor (QWidget):
>     _MUTE = 'MUTE'
> 
>     def __init__ (self, parent):
>         QWidget.__init__ (self, parent)
> 
>         self.setAutoFillBackground (True)
>         lo = QHBoxLayout()
>         lo.setSpacing(4)
> 
>         self._cbFoo = QComboBox()
>         for x in ["ABC", "DEF", "GHI", "JKL"]:
>             self._cbFoo.addItem(x)
> 
>         self._leBar = QLineEdit('', self)
>         lo.addWidget (self._cbFoo, 3)
>         lo.addSpacing (5)
>         lo.addWidget (QLabel ( 'Bar:'))
>         lo.addWidget (self._leBar, 3)
>         lo.addStretch (5)
>         self.setLayout (lo)
> 
> class Form (QDialog):
>     def __init__(self,parent=None):
>         QDialog.__init__(self, parent)
> 
>         grid = QGridLayout ()
>         tree = QTreeWidget ()
> 
>         # Here is the issue?
>         tree.setDragDropMode(QAbstractItemView.InternalMove)
> 
>         tree.setColumnCount(3)
> 
>         for n in range (2):
>             i = QTreeWidgetItem (tree) # create QTreeWidget the sub i
>             i.setText (0, "first" + str (n)) # set the text of the first
0
>             i.setText (1, "second")
>             for m in range (2):
>                 j = QTreeWidgetItem(i)
>                 j.setText (0, "child first" + str (m))
> 
>         #b1 = QCheckBox("push me 0", tree) # this wont work w/ drag by
> itself either
>         #tree.setItemWidget (tree.topLevelItem(0).child(1), 1, b1)
> 
>         item = InlineEditor(tree) # deal with a combination of multiple
>         controls
>         tree.setItemWidget(tree.topLevelItem(0).child(1), 1, item)
> 
>         grid.addWidget (tree)
>         self.setLayout (grid)
> 
> app = QApplication ([])
> form = Form ()
> form.show ()
> app.exec_ ()

If it's a bug then it's not a PyQt bug as a C++ test does the same thing.

Phil


More information about the PyQt mailing list