title: Qt: Attempt to add Drag and Drop to Editable Tree Model example not working
body: <p>I'm trying to learn how to implement Drag and Drop to model/view settings in Qt. As an exercise, I attempted to do that to the <a href="https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html" rel="nofollow noreferrer">Editable Tree Model example</a> available at the Qt web site:</p>
  <p><a href="https://i.stack.imgur.com/Rez27.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rez27.png" alt="Editable Tree Model" /></a></p>
  <p>To extend it with Drag and Drop, I followed the instructions in Qt's documentation on <a href="https://doc.qt.io/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views" rel="nofollow noreferrer">&quot;Using Drag and Drop with View Items&quot;</a>, more specifically <a href="https://doc.qt.io/qt-5/model-view-programming.html#using-model-view-classes" rel="nofollow noreferrer">&quot;Using model/view classes&quot;</a>.</p>
  <p>I placed the code for my attempt at <a href="https://github.com/rodrigodesalvobraz/editabletreemodelwithdraganddrop" rel="nofollow noreferrer">a GitHub repository</a>. The main modifications are below but there are other important ones; <a href="https://github.com/rodrigodesalvobraz/editabletreemodelwithdraganddrop/commit/7ccaa8511ed95267499a3e8852a3450d41a06b53" rel="nofollow noreferrer">here are the full changes</a> according to the documentation.</p>
  <pre><code>/* mainwindow.cpp -- THIS IS NOT ALL CHANGES -- See link above for all changes */
  view-&gt;setSelectionMode(QAbstractItemView::SingleSelection);
  view-&gt;setDragEnabled(true);
  view-&gt;viewport()-&gt;setAcceptDrops(true);
  view-&gt;setDropIndicatorShown(true);
  view-&gt;setDragDropMode(QAbstractItemView::DragDrop);
  </code></pre>
  <p>However, this didn't quite work. While I can drag and drop items, the copied item appears as blank. This can be seen in <a href="https://github.com/rodrigodesalvobraz/editabletreemodelwithdraganddrop/blob/master/Editable%20Tree%20Model%20with%20Drag%20and%20Drop%20problem.mp4?raw=true" rel="nofollow noreferrer">this screen capture</a>, but the main screenshots are:</p>
  <p><a href="https://i.stack.imgur.com/VIFXf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VIFXf.png" alt="Dragging &quot;Getting Started&quot; on &quot;Composing the Dialog&quot;" /></a></p>
  <p><a href="https://i.stack.imgur.com/Ox2Ur.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ox2Ur.png" alt="Results in a blank item" /></a></p>
  <p>Note that the documentation describes the need to re-implement <code>QAbstractItemModel</code>'s <code>dropMimeData</code> for drag and drop functionality, which I did not. This is because, upon inspecting the <a href="https://github.com/qt/qtbase/blob/52077d4f0193a236eacac98f75994b44a4c30a91/src/corelib/itemmodels/qabstractitemmodel.cpp" rel="nofollow noreferrer">source code for that class</a>, I find that <a href="https://github.com/qt/qtbase/blob/52077d4f0193a236eacac98f75994b44a4c30a91/src/corelib/itemmodels/qabstractitemmodel.cpp#L2231" rel="nofollow noreferrer">its default implementation</a> should already work in copying items in drag and drop, since it <a href="https://github.com/qt/qtbase/blob/52077d4f0193a236eacac98f75994b44a4c30a91/src/corelib/itemmodels/qabstractitemmodel.cpp#L2231" rel="nofollow noreferrer">uses the default</a> <code>application/x-qabstractitemmodeldatalist</code> MIME format and <a href="https://github.com/qt/qtbase/blob/52077d4f0193a236eacac98f75994b44a4c30a91/src/corelib/itemmodels/qabstractitemmodel.cpp#L2859" rel="nofollow noreferrer">uses <code>setItemData</code></a> for the inserted items.</p>
  <p>What is wrong here? Is it the default <code>dropMimeData</code> that's not working, or something else?</p>