title: Drag and drop rows within QTableWidget
body: <p><strong>Goal</strong></p>
  
  <p>My goal is to have a <code>QTableWidget</code> in which the user can drag/drop rows internally. That is, the user can drag and drop one entire row, moving it up or down in the table to a different location in between two other rows.  The goal is illustrated in this figure:</p>
  
  <p><img src="https://i.stack.imgur.com/TG0iz.jpg" alt="the challenge"></p>
  
  <p><strong>What I tried, and what happens</strong> </p>
  
  <p>Once I have populated a <code>QTableWidget</code> with data, I set its properties as follows:</p>
  
  <pre><code>table.setDragDropMode(QtGui.QAbstractItemView.InternalMove)   
  #select one row at a time
  table.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) 
  table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
  </code></pre>
  
  <p>Similar code makes <code>QListWidget</code> behave nicely: when you move an item internally, it is dropped between two elements of the list, and the rest of the items sort themselves out in a reasonable way, with no data overwritten (in other words, the view acts like the figure above, but it is a list). </p>
  
  <p>In contrast, in a table modified with the code above, things don't work out as planned. The following figure shows what actually happens:</p>
  
  <p><img src="https://i.stack.imgur.com/ixZup.jpg" alt="crud"></p>
  
  <p>In words: when row <em>i</em> is dropped, that row becomes blank in the table. Further, if I accidentally drop row <em>i</em> onto row <em>j</em> (instead of the space between two rows), the data from row <em>i</em> <em>replaces</em> the data in row <em>j</em>. That is, in that unfortunate case, in addition to row <em>i</em> becoming blank, row <em>j</em> is overwritten. </p>
  
  <p>Note I also tried adding <code>table.setDragDropOverwriteMode(False)</code> but it didn't change the behavior.</p>
  
  <p><strong>A way forward?</strong></p>
  
  <p><a href="https://bugreports.qt-project.org/browse/QTBUG-13873?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel" rel="noreferrer">This bug report</a> might include a possible solution in C++: it seems they reimplemented <code>dropEvent</code> for <code>QTableWidget</code>, but I am not sure how to cleanly port to Python. </p>
  
  <p><strong>Related content:</strong></p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/7666823/reordering-items-in-a-qtreewidget-with-drag-and-drop-in-pyqt">Reordering items in a QTreeWidget with Drag and Drop in PyQt</a></li>
  <li><a href="https://stackoverflow.com/questions/21842342/qt-internal-drag-and-drop-of-rows-in-qtableview-that-changes-order-of-rows-in?rq=1">QT: internal drag and drop of rows in QTableView, that changes order of rows in QTableModel</a></li>
  <li><a href="http://www.qtcentre.org/threads/35113-QTableWidget-dropping-between-the-rows-shall-insert-the-item" rel="noreferrer">http://www.qtcentre.org/threads/35113-QTableWidget-dropping-between-the-rows-shall-insert-the-item</a></li>
  <li><a href="https://stackoverflow.com/questions/4252114/qt-pyqt-qtreeview-internal-drag-and-drop-almost-working-dragged-item-disapp">qt: pyqt: QTreeView internal drag and drop almost working... dragged item disappears</a></li>
  <li><a href="http://qt-project.org/forums/viewthread/9925" rel="noreferrer">How to drag &amp; drop rows within QTableWidget</a> </li>
  <li><a href="https://stackoverflow.com/questions/4536439/qlistwidget-drag-and-drop-items-disappearing-from-list-on-symbian">QListWidget drag and drop items disappearing from list on Symbian</a></li>
  <li><a href="http://www.qtcentre.org/threads/52438-QTableWidget-Internal-Drag-Drop-Entire-Row" rel="noreferrer">QTableWidget Internal Drag Drop Entire Row</a></li>
  </ul>