<?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[Move qtreeview rows]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I have a qtreeview with a qstandarditemmodel..</p>
<p dir="auto">I would move a row with all subentries to a new position.</p>
<p dir="auto">I tried:<br />
@    moveRow( customerItem-&gt;parent()-&gt;index(), customerItem-&gt;row(),<br />
_customerGroupsToItems[groupId]-&gt;index(), 0);@</p>
<p dir="auto">And now, i readed in the documentation auf moveRows:<br />
@The base class implementation does nothing and returns false.@</p>
<p dir="auto">How can i solve my issue?</p>
]]></description><link>https://forum.qt.io/topic/30085/move-qtreeview-rows</link><generator>RSS for Node</generator><lastBuildDate>Tue, 17 Nov 2026 22:38:50 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/30085.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 30 Jul 2013 13:50:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Move qtreeview rows on Fri, 26 Jan 2018 04:08:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/diracsbracket">@<bdi>Diracsbracket</bdi></a><br />
Although this seems to work correctly in the QTreeView, I get the error messages in the Debugger as explained in this post.<br />
<a href="https://forum.qt.io/topic/86021/qabstractitemmodel-endmoverows-invalid-index">https://forum.qt.io/topic/86021/qabstractitemmodel-endmoverows-invalid-index</a></p>
<p dir="auto">Update: The problem I mentioned in that post was solved by @proman. I should have taken the time to read Qt's online Model/View Programming documentation, as it is clearly mentioned there...</p>
]]></description><link>https://forum.qt.io/post/432034</link><guid isPermaLink="true">https://forum.qt.io/post/432034</guid><dc:creator><![CDATA[Diracsbracket]]></dc:creator><pubDate>Fri, 26 Jan 2018 04:08:25 GMT</pubDate></item><item><title><![CDATA[Reply to Move qtreeview rows on Sat, 16 Dec 2017 03:34:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/david-stiel">@<bdi>David-Stiel</bdi></a><br />
Thanks!<br />
I adapted your example to work with a <code>QTreeView</code> with more than 1 column, as follows (only 1 step up/down per invocation)</p>
<pre><code>bool StandardItemModel::move(int from, bool isUp)
{
    //range checking etc.
    const int numRows = rowCount();

    if (from&lt;0 || from&gt;=numRows || (from==0 &amp;&amp; isUp) || (from==numRows-1 &amp;&amp; !isUp))
        return false;

    const int to = isUp ? from - 1: from + 1;

    //See documentation
    //http://doc.qt.io/qt-5/qabstractitemmodel.html#beginMoveRows
    //on why this is needed?
    const int toSignal = isUp ? to : to + 1;

    if (!beginMoveRows(QModelIndex(), from, from, QModelIndex(), toSignal))
        return false;

    blockSignals(true);
    QList&lt;QStandardItem*&gt; fromRow = takeRow(from); //this also removes it from the model!
    insertRow(to, fromRow); //but the actual insertion must be done using to, not toSignal!
    blockSignals(false);

    endMoveRows();

    return true;
}
</code></pre>
<p dir="auto">Note that there is no <code>removeRow(from)</code> in the adapted code, as <code>takeRow(from)</code> seems to already take care of that.<br />
In a dialog, I have <code>Move Up</code> and <code>Move Down</code> buttons that call the following dialog method:</p>
<pre><code>void changeOrderDialog::moveCurrentRow(bool isUp)
{
    const QModelIndex index = ui-&gt;treeView-&gt;currentIndex();

    if (index.isValid())
    {
        StandardItemModel* model = qobject_cast&lt;StandardItemModel*&gt;(ui-&gt;treeView-&gt;model());
        const int row = index.row();

        if (model-&gt;move(row, isUp))
        {
            const QModelIndex newIndex = model-&gt;index(isUp ? row-1 : row+1, 0);
            ui-&gt;treeView-&gt;setCurrentIndex(newIndex);
        }
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/post/431825</link><guid isPermaLink="true">https://forum.qt.io/post/431825</guid><dc:creator><![CDATA[Diracsbracket]]></dc:creator><pubDate>Sat, 16 Dec 2017 03:34:59 GMT</pubDate></item><item><title><![CDATA[Reply to Move qtreeview rows on Fri, 07 Nov 2014 11:18:25 GMT]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I had the same problem moving a row within a  flat QStandardItemModel. It took some figuring out to get the move operation working properly with eg. QML ListView. I ended up doing:</p>
<p dir="auto">@void MyModel::move(int from, int to, int)<br />
{<br />
... //range checking etc.</p>
<pre><code>int toSignal = to;
if(from &lt; to){
    toSignal += 1;
}

if(!beginMoveRows(QModelIndex(), from, from, QModelIndex(), toSignal)){
    return;
}
blockSignals(true);
QStandardItem* fromItem = takeItem(from);
removeRow(from);
insertRow(to, fromItem);
blockSignals(false);
endMoveRows();
</code></pre>
<p dir="auto">}@</p>
]]></description><link>https://forum.qt.io/post/201635</link><guid isPermaLink="true">https://forum.qt.io/post/201635</guid><dc:creator><![CDATA[David Stiel]]></dc:creator><pubDate>Fri, 07 Nov 2014 11:18:25 GMT</pubDate></item><item><title><![CDATA[Reply to Move qtreeview rows on Sat, 03 Aug 2013 21:52:16 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">This is because QStandardItemModel doesn't know anything about your underlying data so it can't move things around for you.</p>
<p dir="auto">You have to implement moveRows for your model if you want to use moveRow.</p>
<p dir="auto">Hope it helps</p>
]]></description><link>https://forum.qt.io/post/189363</link><guid isPermaLink="true">https://forum.qt.io/post/189363</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 03 Aug 2013 21:52:16 GMT</pubDate></item></channel></rss>