<?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[Receiving notification on internal movement of items inside a QTreeWidget]]></title><description><![CDATA[<p dir="auto">I subclassed QTreeWidget and set dragDropMode to InternalMove. Moving items inside the tree works as expected, but I need to react to the items changing their order. Unfortunately, there's no signal that I could connect to that would let me do that.</p>
<p dir="auto">I obtained the handle to the underlying model via QTreeWidget::model() and connected to the model's rowsMoved() signal, but unfortunately, it doesn't seem to be emitted for internal moves.</p>
<p dir="auto">I also tried reimplementing dropEvent(), but I can't find a way to reliably obtain the destination row index there. From event-&gt;pos() I can obtain a QModelIndex of the item under the cursor, but this is useless, because while the drop indicator stays in place between two items, I get the index of the one above if I move the mouse a pixel up, and the one below if I move it down - even though the destination of the move is the same.</p>
<p dir="auto">How can I receive notifications of item moves and react to them?</p>
]]></description><link>https://forum.qt.io/topic/29563/receiving-notification-on-internal-movement-of-items-inside-a-qtreewidget</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Apr 2026 15:25:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/29563.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 17 Jul 2013 21:12:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Receiving notification on internal movement of items inside a QTreeWidget on Wed, 17 Jul 2013 23:54:59 GMT]]></title><description><![CDATA[<p dir="auto">currentItemChanged() gets triggered, but it doesn't refer to the item that moved, but the one that occupies the space that it used to hold, so it's useless. Even more so when you move multiple items.</p>
<p dir="auto">I also figured out an another way to figure out the row numbers of the moving items from before and after the move, which is based on indexFromItem() like your suggestion:</p>
<p dir="auto">@<br />
void MyTreeWidget::dropEvent(QDropEvent <em>event)<br />
{<br />
// get the list of the items that are about to be dropped<br />
QList&lt;QTreeWidgetItem</em>&gt; dragItems = selectedItems();</p>
<pre><code>// find out their row numbers before the drop
QList&lt;int&gt; fromRows;
QTreeWidgetItem *item;
foreach(item, dragItems) fromRows.append(indexFromItem(item).row());

// the default implementation takes care of the actual move inside the tree
QTreeWidget::dropEvent(event);

// query the indices of the dragged items again after the drop
QList&lt;int&gt; toRows;
foreach(item, dragItems) toRows.append(indexFromItem(item).row());

// notify subscribers in some useful way
emit itemsMoved(fromRows, toRows);
</code></pre>
<p dir="auto">}<br />
@</p>
]]></description><link>https://forum.qt.io/post/186873</link><guid isPermaLink="true">https://forum.qt.io/post/186873</guid><dc:creator><![CDATA[neuviemep]]></dc:creator><pubDate>Wed, 17 Jul 2013 23:54:59 GMT</pubDate></item><item><title><![CDATA[Reply to Receiving notification on internal movement of items inside a QTreeWidget on Wed, 17 Jul 2013 23:46:56 GMT]]></title><description><![CDATA[<p dir="auto">sorry for the late response, but I'm glad you got it</p>
]]></description><link>https://forum.qt.io/post/186872</link><guid isPermaLink="true">https://forum.qt.io/post/186872</guid><dc:creator><![CDATA[Buckets]]></dc:creator><pubDate>Wed, 17 Jul 2013 23:46:56 GMT</pubDate></item><item><title><![CDATA[Reply to Receiving notification on internal movement of items inside a QTreeWidget on Wed, 17 Jul 2013 23:46:00 GMT]]></title><description><![CDATA[<p dir="auto">I'm not sure if currentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)  gets triggered if you change the item's position, but it is worth a shot.</p>
<p dir="auto">You could also extend the QTreeWidget with a class that holds the current QModelIndex (cur) of the current pointer and the previous position (pre).</p>
<p dir="auto">When you do the internal movement set pre =cur, cur = indexFromItem(QTreeWidgtItem).<br />
Do a check to see if pre != cur or if pre!= some default value, then emit(moved_item(pre, cur)) and set pre to cur or a default value.</p>
]]></description><link>https://forum.qt.io/post/186871</link><guid isPermaLink="true">https://forum.qt.io/post/186871</guid><dc:creator><![CDATA[Buckets]]></dc:creator><pubDate>Wed, 17 Jul 2013 23:46:00 GMT</pubDate></item><item><title><![CDATA[Reply to Receiving notification on internal movement of items inside a QTreeWidget on Wed, 17 Jul 2013 22:52:08 GMT]]></title><description><![CDATA[<p dir="auto">I figured it out:</p>
<p dir="auto">@<br />
void ProjectTree::dropEvent(QDropEvent *event)<br />
{<br />
// retrieve the target of the drop<br />
QTreeWidgetItem *target = itemAt(event-&gt;pos());<br />
DropIndicatorPosition dropPos = dropIndicatorPosition();<br />
// when the drop indicator points above an item, then this is the<br />
// correct one, otherwise, replace target item with the one below<br />
if (dropPos == BelowItem) target = itemBelow(target);</p>
<p dir="auto">// get list of selected items from the tree; these are the dragged<br />
// items<br />
QList&lt;QTreeWidgetItem*&gt; dragItems = selectedItems();</p>
<p dir="auto">// default implementation takes care of the actual move<br />
QTreeWidget::dropEvent(event);<br />
// notify subscribers of item move in some user-friendly way<br />
emit itemsMoved(...)<br />
}<br />
@</p>
]]></description><link>https://forum.qt.io/post/186869</link><guid isPermaLink="true">https://forum.qt.io/post/186869</guid><dc:creator><![CDATA[neuviemep]]></dc:creator><pubDate>Wed, 17 Jul 2013 22:52:08 GMT</pubDate></item><item><title><![CDATA[Reply to Receiving notification on internal movement of items inside a QTreeWidget on Wed, 17 Jul 2013 22:29:31 GMT]]></title><description><![CDATA[<p dir="auto">The problem is not that I don't know how to emit my own signals, but that I have trouble finding out which item(s) moved exactly where.</p>
<p dir="auto">The way I see it, since QTreeWidget has signals like itemClicked(), itemChanged() etc., it should also have itemMoved(from, to) which would let me react to the move easily.</p>
]]></description><link>https://forum.qt.io/post/186866</link><guid isPermaLink="true">https://forum.qt.io/post/186866</guid><dc:creator><![CDATA[neuviemep]]></dc:creator><pubDate>Wed, 17 Jul 2013 22:29:31 GMT</pubDate></item><item><title><![CDATA[Reply to Receiving notification on internal movement of items inside a QTreeWidget on Wed, 17 Jul 2013 22:01:04 GMT]]></title><description><![CDATA[<p dir="auto">I take it you are dynamically changing the order of the QTreeWidget when given certain data, or sorting.</p>
<p dir="auto">When you reimplemented the dropEvent() are you emitting a signal?  If there isn't a signal you like you can always make your own.</p>
<p dir="auto">i'm not sure if this will help you at all: <a href="http://qt-project.org/wiki/Qt_for_beginners_Signals_and_slots_2" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/wiki/Qt_for_beginners_Signals_and_slots_2</a></p>
]]></description><link>https://forum.qt.io/post/186864</link><guid isPermaLink="true">https://forum.qt.io/post/186864</guid><dc:creator><![CDATA[Buckets]]></dc:creator><pubDate>Wed, 17 Jul 2013 22:01:04 GMT</pubDate></item></channel></rss>