<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Item gets duplicated in the model after drag-n-drop in QTreeView]]></title><description><![CDATA[<p dir="auto">I have a <code>QStandardItemModel()</code> with a number of <code>QStandardItem()</code> elements displayed in a customized <code>QTreeView()</code>.<br />
It's a simple tree, and after the element is dragged and dropped I need to save this change to the database.<br />
The drag-n-drop mode set to <code>QAbstractItemView.InternalMove</code><br />
Now here's the problem that I'm facing.<br />
Suppose we had a tree:</p>
<pre><code>parent
- childA
- childB
- childC
</code></pre>
<p dir="auto">And dragged <code>childC</code> above <code>childB</code>:</p>
<pre><code>parent
- childA
- childC
- childB
</code></pre>
<p dir="auto">I'm listing the items using this code:</p>
<pre><code>self.model.itemChanged.connect(self.item_changed)
</code></pre>
<p dir="auto">...</p>
<pre><code>def item_changed(self, item):
    par = item.parent()
        for i in range(0,par.rowCount()):
            print(i,par.child(i).text()) 
</code></pre>
<p dir="auto">And it returns this:</p>
<pre><code>childA
childC
childB
childC
</code></pre>
<p dir="auto">I.e. it lists the dragged element twice - at its previous position and at its current position. In the tree everything is fine, the items are not duplicated.</p>
<p dir="auto">Why is that? How can I reliably track the reorder operations like this to save this state?<br />
Thanks.</p>
<p dir="auto">P.S. I also saw the same problem when I tried to get the data from the model inside <code>dropEvent()</code> of my <code>QTreeView()</code> implementation.</p>
]]></description><link>https://forum.qt.io/topic/124455/item-gets-duplicated-in-the-model-after-drag-n-drop-in-qtreeview</link><generator>RSS for Node</generator><lastBuildDate>Sun, 14 Jun 2026 09:45:21 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/124455.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 05 Mar 2021 17:56:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Item gets duplicated in the model after drag-n-drop in QTreeView on Fri, 05 Mar 2021 18:57:47 GMT]]></title><description><![CDATA[<p dir="auto">Looks like this is the expected behavior, and it's described here: <a href="https://stackoverflow.com/questions/52873025/pyqt5-qlistview-drag-and-drop-creates-new-hidden-items" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/52873025/pyqt5-qlistview-drag-and-drop-creates-new-hidden-items</a></p>
<p dir="auto">When an item moves from position <code>i</code> to position <code>j</code>, what is done is:</p>
<ul>
<li>Insert an item in position <code>j</code></li>
<li>Copy the data to the new item, at this moment the itemChanged signal is emitted and therefore you see that there are elements of more</li>
<li>Delete the item that was in the position <code>i</code>.</li>
</ul>
<p dir="auto">So my question is: which method should I use to save the data <em>after</em> the item in the position <code>i</code> was deleted?</p>
<p dir="auto">At the moment it looks like I have to save the data in one of the 2 places:</p>
<ul>
<li>in <code>dropEvent()</code> of my TreeView</li>
<li>in a method called by <code>itemChanged()</code> of the model, where I'll have to compare the model indexes to distinguish between the new and the old position of the element</li>
</ul>
]]></description><link>https://forum.qt.io/post/647911</link><guid isPermaLink="true">https://forum.qt.io/post/647911</guid><dc:creator><![CDATA[midnightdim]]></dc:creator><pubDate>Fri, 05 Mar 2021 18:57:47 GMT</pubDate></item></channel></rss>