<p>I'm trying to keep a widget put into a QTreeWidgetItem after a reparent (drag and drop) using QTreeWidget.setItemWidget()</p>
<p>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?)<br>
</p>
<pre class="prettyprint"><code><span class="kwd"><br><br></span></code><code><span class="kwd">from</span><span class="pln"> </span><span class="typ">PyQt4</span><span class="pun">.</span><span class="typ">QtCore</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> </span><span class="pun">*</span><span class="pln"><br>
</span><span class="kwd">from</span><span class="pln"> </span><span class="typ">PyQt4</span><span class="pun">.</span><span class="typ">QtGui</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> </span><span class="pun">*</span><span class="pln"><br>
<br><br></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">InlineEditor</span><span class="pln"> </span><span class="pun">(</span><span class="typ">QWidget</span><span class="pun">):</span><span class="pln"><br>
_MUTE </span><span class="pun">=</span><span class="pln"> </span><span class="str">'MUTE'</span><span class="pln"><br><br> </span><span class="kwd">def</span><span class="pln"> __init__ </span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> parent</span><span class="pun">):</span><span class="pln"><br>
</span><span class="typ">QWidget</span><span class="pun">.</span><span class="pln">__init__ </span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> parent</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setAutoFillBackground </span><span class="pun">(</span><span class="kwd">True</span><span class="pun">)</span><span class="pln"><br>
lo </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QHBoxLayout</span><span class="pun">()</span><span class="pln"><br> lo</span><span class="pun">.</span><span class="pln">setSpacing</span><span class="pun">(</span><span class="lit">4</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">_cbFoo </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QComboBox</span><span class="pun">()</span><span class="pln"><br>
</span><span class="kwd">for</span><span class="pln"> x </span><span class="kwd">in</span><span class="pln"> </span><span class="pun">[</span><span class="str">"ABC"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"DEF"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"GHI"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"JKL"</span><span class="pun">]:</span><span class="pln"><br>
</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">_cbFoo</span><span class="pun">.</span><span class="pln">addItem</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span><span class="pln"><br>
<br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">_leBar </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QLineEdit</span><span class="pun">(</span><span class="str">''</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">)</span><span class="pln"><br>
lo</span><span class="pun">.</span><span class="pln">addWidget </span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">_cbFoo</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">)</span><span class="pln"><br>
lo</span><span class="pun">.</span><span class="pln">addSpacing </span><span class="pun">(</span><span class="lit">5</span><span class="pun">)</span><span class="pln"><br> lo</span><span class="pun">.</span><span class="pln">addWidget </span><span class="pun">(</span><span class="typ">QLabel</span><span class="pln"> </span><span class="pun">(</span><span class="pln"> </span><span class="str">'Bar:'</span><span class="pun">))</span><span class="pln"><br>
lo</span><span class="pun">.</span><span class="pln">addWidget </span><span class="pun">(</span><span class="kwd">self</span><span class="pun">.</span><span class="pln">_leBar</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">)</span><span class="pln"><br>
lo</span><span class="pun">.</span><span class="pln">addStretch </span><span class="pun">(</span><span class="lit">5</span><span class="pun">)</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setLayout </span><span class="pun">(</span><span class="pln">lo</span><span class="pun">)</span><span class="pln"><br>
<br></span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Form</span><span class="pln"> </span><span class="pun">(</span><span class="typ">QDialog</span><span class="pun">):</span><span class="pln"><br>
</span><span class="kwd">def</span><span class="pln"> __init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln">parent</span><span class="pun">=</span><span class="kwd">None</span><span class="pun">):</span><span class="pln"><br>
</span><span class="typ">QDialog</span><span class="pun">.</span><span class="pln">__init__</span><span class="pun">(</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> parent</span><span class="pun">)</span><span class="pln"><br>
<br> grid </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QGridLayout</span><span class="pln"> </span><span class="pun">()</span><span class="pln"><br> tree </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QTreeWidget</span><span class="pln"> </span><span class="pun">()</span><span class="pln"><br>
<br> </span><span class="com"># Here is the issue?</span><span class="pln"><br> tree</span><span class="pun">.</span><span class="pln">setDragDropMode</span><span class="pun">(</span><span class="typ">QAbstractItemView</span><span class="pun">.</span><span class="typ">InternalMove</span><span class="pun">)</span><span class="pln"><br>
<br> tree</span><span class="pun">.</span><span class="pln">setColumnCount</span><span class="pun">(</span><span class="lit">3</span><span class="pun">)</span><span class="pln"><br><br> </span><span class="kwd">for</span><span class="pln"> n </span><span class="kwd">in</span><span class="pln"> range </span><span class="pun">(</span><span class="lit">2</span><span class="pun">):</span><span class="pln"><br>
i </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QTreeWidgetItem</span><span class="pln"> </span><span class="pun">(</span><span class="pln">tree</span><span class="pun">)</span><span class="pln"> </span><span class="com"># create QTreeWidget the sub i</span><span class="pln"><br>
i</span><span class="pun">.</span><span class="pln">setText </span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="str">"first"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str </span><span class="pun">(</span><span class="pln">n</span><span class="pun">))</span><span class="pln"> </span><span class="com"># set the text of the first 0</span><span class="pln"><br>
i</span><span class="pun">.</span><span class="pln">setText </span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="str">"second"</span><span class="pun">)</span><span class="pln"><br>
</span><span class="kwd">for</span><span class="pln"> m </span><span class="kwd">in</span><span class="pln"> range </span><span class="pun">(</span><span class="lit">2</span><span class="pun">):</span><span class="pln"><br>
j </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QTreeWidgetItem</span><span class="pun">(</span><span class="pln">i</span><span class="pun">)</span><span class="pln"><br> j</span><span class="pun">.</span><span class="pln">setText </span><span class="pun">(</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> </span><span class="str">"child first"</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str </span><span class="pun">(</span><span class="pln">m</span><span class="pun">))</span><span class="pln"><br>
<br> </span><span class="com">#b1 = QCheckBox("push me 0", tree) # this wont work w/ drag by itself either</span><span class="pln"><br> </span><span class="com">#tree.setItemWidget (tree.topLevelItem(0).child(1), 1, b1)</span><span class="pln"><br>
<br> item </span><span class="pun">=</span><span class="pln"> </span><span class="typ">InlineEditor</span><span class="pun">(</span><span class="pln">tree</span><span class="pun">)</span><span class="pln"> </span><span class="com"># deal with a combination of multiple controls</span><span class="pln"><br>
tree</span><span class="pun">.</span><span class="pln">setItemWidget</span><span class="pun">(</span><span class="pln">tree</span><span class="pun">.</span><span class="pln">topLevelItem</span><span class="pun">(</span><span class="lit">0</span><span class="pun">).</span><span class="pln">child</span><span class="pun">(</span><span class="lit">1</span><span class="pun">),</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> item</span><span class="pun">)</span><span class="pln"><br>
<br> grid</span><span class="pun">.</span><span class="pln">addWidget </span><span class="pun">(</span><span class="pln">tree</span><span class="pun">)</span><span class="pln"><br> </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">setLayout </span><span class="pun">(</span><span class="pln">grid</span><span class="pun">)</span><span class="pln"><br>
<br>app </span><span class="pun">=</span><span class="pln"> </span><span class="typ">QApplication</span><span class="pln"> </span><span class="pun">([])</span><span class="pln"><br>form </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Form</span><span class="pln"> </span><span class="pun">()</span><span class="pln"><br>
form</span><span class="pun">.</span><span class="pln">show </span><span class="pun">()</span><span class="pln"><br>app</span><span class="pun">.</span><span class="pln">exec_ </span><span class="pun">()</span><span class="pln"><br>
</span></code><code><span class="kwd"></span><span class="pun"></span><span class="pln"><br></span></code></pre>