<?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[Moving row in QStandardItemModel unhides hidden QTreeView columns?]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I have a <code>QTreeView</code> with a simple  model class derived from <code>QStandardItemModel</code> which allows me to move rows up and down in the view. The derived class provides a single extra method called <code>move(...)</code> which does the work. It is implemented as in <a href="https://forum.qt.io/topic/86021/qabstractitemmodel-endmoverows-invalid-index">this post</a>.</p>
<p dir="auto">My model has only two columns, one of which is hidden (the last). The row moving mechanism works fine. However, since I migrated to Qt 5.9.4, moving a row in this way unhides the hidden column!? This did not happen in Qt 5.9.2 (I'm working on Windows 10, compiling with Visual Studio 2017 community).</p>
<p dir="auto">Why does this unhiding happen and how can I keep the hidden column hidden?</p>
]]></description><link>https://forum.qt.io/topic/87177/moving-row-in-qstandarditemmodel-unhides-hidden-qtreeview-columns</link><generator>RSS for Node</generator><lastBuildDate>Fri, 05 Jun 2026 22:05:35 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/87177.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 25 Jan 2018 15:08:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Moving row in QStandardItemModel unhides hidden QTreeView columns? on Tue, 30 Jan 2018 22:36:56 GMT]]></title><description><![CDATA[<p dir="auto">From time to time, subtle regression can happen.</p>
<p dir="auto">You might want to check the <a href="https://bugreports.qt.io" target="_blank" rel="noopener noreferrer nofollow ugc">bug report system</a> to see if there's something related.</p>
]]></description><link>https://forum.qt.io/post/439433</link><guid isPermaLink="true">https://forum.qt.io/post/439433</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 30 Jan 2018 22:36:56 GMT</pubDate></item><item><title><![CDATA[Reply to Moving row in QStandardItemModel unhides hidden QTreeView columns? on Tue, 30 Jan 2018 10:16:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> said in <a href="/post/438910">Moving row in QStandardItemModel unhides hidden QTreeView columns?</a>:</p>
<blockquote>
<p dir="auto">Can you check with 5.10 ? Seems to work correctly</p>
</blockquote>
<p dir="auto">Well, of course I could do that, and from what you wrote, it seems to work there.</p>
<p dir="auto">But then, I don't really understand the point of releasing the patch release 5.9.4, if in addition to solving bugs, introduces new ones, such as this one...</p>
<p dir="auto">EDIT: Just checked: it works correctly in 5.10</p>
]]></description><link>https://forum.qt.io/post/439280</link><guid isPermaLink="true">https://forum.qt.io/post/439280</guid><dc:creator><![CDATA[Diracsbracket]]></dc:creator><pubDate>Tue, 30 Jan 2018 10:16:30 GMT</pubDate></item><item><title><![CDATA[Reply to Moving row in QStandardItemModel unhides hidden QTreeView columns? on Sat, 27 Jan 2018 20:47:01 GMT]]></title><description><![CDATA[<p dir="auto">Can you check with 5.10 ? Seems to work correctly.</p>
]]></description><link>https://forum.qt.io/post/438910</link><guid isPermaLink="true">https://forum.qt.io/post/438910</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 27 Jan 2018 20:47:01 GMT</pubDate></item><item><title><![CDATA[Reply to Moving row in QStandardItemModel unhides hidden QTreeView columns? on Fri, 26 Jan 2018 06:06:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a><br />
I tried uploading a minimal example in a zip archive, but I  have not enough privileges to do so. I therefore add the code here.</p>
<p dir="auto">The <code>mainwindow.ui</code> form contains a single <code>QTreeView</code> item, named <code>treeView</code>, and two buttons named <code>upButton</code> and <code>downButton</code>.</p>
<p dir="auto">I can confirm that the the problem occurs with 5.9.4 but not in 5.9.2.</p>
<p dir="auto">dragstandarditemmodel.h:</p>
<pre><code>#ifndef DRAGSTANDARDITEMMODEL_H
#define DRAGSTANDARDITEMMODEL_H

//#include &lt;QAbstractItemModel&gt;
#include &lt;QStandardItemModel&gt;


class DragStandardItemModel : public QStandardItemModel
{
    Q_OBJECT

public:
    DragStandardItemModel(int rows, int columns, QObject *parent = Q_NULLPTR);
    bool move(int from, bool isUp);
};

#endif // DRAGSTANDARDITEMMODEL_H

</code></pre>
<p dir="auto">dragstandarditemmodel.cpp:</p>
<pre><code>#include "dragstandarditemmodel.h"

#include &lt;QDebug&gt;

DragStandardItemModel::DragStandardItemModel(int rows, int columns, QObject *parent)
    : QStandardItemModel(rows, columns, parent)
{

}

bool DragStandardItemModel::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;
    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);
    beginInsertRows(QModelIndex(), toSignal, toSignal);
    insertRow(to, fromRow);

    blockSignals(false);

    endMoveRows();

    return true;
}
</code></pre>
<p dir="auto">mainwindow.h:</p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;

class DragStandardItemModel;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_upButton_clicked();
    void on_downButton_clicked();

private:
    Ui::MainWindow *ui;
    DragStandardItemModel* m_colModel;

    void moveCurrentRow(bool isUp);
};

#endif // MAINWINDOW_H
</code></pre>
<p dir="auto">mainwindow.cpp:</p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "dragstandarditemmodel.h"

#include &lt;QStandardItem&gt;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    //Create model, fill some data, hide 2nd column
    const int numRows = 10;
    const int numCols = 2;

    m_colModel = new DragStandardItemModel(numRows, numCols, ui-&gt;treeView);

    for (int i=0; i&lt;numRows; ++i)
    {
        QStandardItem* itm = new QStandardItem(QString::number(i+1));
        itm-&gt;setCheckable(true);
        itm-&gt;setCheckState(Qt::Checked);
        m_colModel-&gt;setItem(i, 0, itm);

        //Hidden column, to contain the logical index of visual item at i
        itm = new QStandardItem(QString::number(i));
        m_colModel-&gt;setItem(i, 1, itm);
    }

    m_colModel-&gt;setHorizontalHeaderLabels({"Info"});
    ui-&gt;treeView-&gt;setModel(m_colModel);
    ui-&gt;treeView-&gt;setColumnHidden(1, true);
    ui-&gt;treeView-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);

    ui-&gt;treeView-&gt;setRootIsDecorated(false);
    ui-&gt;treeView-&gt;resizeColumnToContents(0);
    ui-&gt;treeView-&gt;setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui-&gt;treeView-&gt;adjustSize();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::moveCurrentRow(bool isUp)
{
    const QModelIndex index = ui-&gt;treeView-&gt;currentIndex();

    if (index.isValid())
    {
        const int row = index.row();

        if (m_colModel-&gt;move(row, isUp))
        {
            const QModelIndex newIndex = m_colModel-&gt;index(isUp ? row-1 : row+1, 0);
            ui-&gt;treeView-&gt;setCurrentIndex(newIndex);
        }
    }
}

void MainWindow::on_upButton_clicked()
{
    moveCurrentRow(true);
}

void MainWindow::on_downButton_clicked()
{
    moveCurrentRow(false);
}
</code></pre>
<p dir="auto">For now, I just re-hide the column after each move, by adding the following line before the <code>setCurrentIndex()</code> operation:</p>
<pre><code>ui-&gt;treeView-&gt;setColumnHidden(1, true);
</code></pre>
<p dir="auto">But this is just an uggly temp workaround.</p>
]]></description><link>https://forum.qt.io/post/438635</link><guid isPermaLink="true">https://forum.qt.io/post/438635</guid><dc:creator><![CDATA[Diracsbracket]]></dc:creator><pubDate>Fri, 26 Jan 2018 06:06:26 GMT</pubDate></item><item><title><![CDATA[Reply to Moving row in QStandardItemModel unhides hidden QTreeView columns? on Thu, 25 Jan 2018 22:10:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Can you show a minimal compilable version that can reused to test that behaviour ?</p>
]]></description><link>https://forum.qt.io/post/438620</link><guid isPermaLink="true">https://forum.qt.io/post/438620</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 25 Jan 2018 22:10:58 GMT</pubDate></item></channel></rss>