<?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[TreeView not updating after a dataChanged signal]]></title><description><![CDATA[<p dir="auto">Everything is in the title, i have a treeview displaying a custom model.<br />
I'm trying to edit some data in it but the dataChanged signal doesn't seems to work.</p>
<p dir="auto">I've tried with the begin/endResetModel which is working fine.<br />
I also noticed that the edited field is updating if I hide it thew reshow it by dragging the treeview out of the layer (as the content of the treeview draggable).</p>
<p dir="auto">I've also tried to send the signal on the root or on the parents.<br />
Things like that :</p>
<pre><code>            QModelIndex rootIndex = createIndex(0, 0, m_root);
            QModelIndex rootIndex2 = createIndex(m_root-&gt;childCount(), m_root-&gt;columnCount(), m_root);
            emit dataChanged(rootIndex, rootIndex2);
</code></pre>
<p dir="auto">but with no results.</p>
<p dir="auto">I'm pretty sure there is some subtilty i didn't get but i can't find out so i need your help =)</p>
<p dir="auto">Here's a simplified version of what i'm doing :<br />
-In my tree model :</p>
<pre><code>bool MapMarkerTreeModel::setData(const QModelIndex&amp; _index, const QVariant&amp; _value, int _role/* = Qt::EditRole*/)
{
    if (!_index.isValid())
    {
        return false;
    }

    if(MapMarkerTreeItem* item = getItem(_index))
    {
        if(item-&gt;setData(_value, _role))
        {
//            beginResetModel(); &lt;----- This is working
//            endResetModel();
            emit dataChanged(_index, _index); // &lt;----- This isn't

            return true;
        }
    }

    return false;
}
</code></pre>
<p dir="auto">-Also in my tree model, index and parent methods :</p>
<pre><code>QModelIndex MapMarkerTreeModel::index(const MapMarkerTreeItem* _item) const
{
    QModelIndex index{};
    if(_item != nullptr)
    {
        index = createIndex(_item-&gt;childNumber(), 0, _item);
    }

    return index;
}

QModelIndex MapMarkerTreeModel::parent(const QModelIndex&amp; _index) const
{
    if (!_index.isValid() || m_root == nullptr)
    {
        return QModelIndex();
    }

    MapMarkerTreeItem* childItem = getItem(_index);
    MapMarkerTreeItem* parentItem = childItem ? childItem-&gt;parent() : nullptr;

    if (!parentItem)
    {
        return QModelIndex();
    }

    return createIndex(parentItem-&gt;childNumber(), 0, parentItem);
}
</code></pre>
<p dir="auto">I'm not sure what else I can provide for context, if anything can help please tell me.<br />
And thanks in advance !</p>
]]></description><link>https://forum.qt.io/topic/149316/treeview-not-updating-after-a-datachanged-signal</link><generator>RSS for Node</generator><lastBuildDate>Sat, 16 May 2026 12:46:36 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/149316.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Sep 2023 11:21:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Tue, 31 Mar 2026 08:27:28 GMT]]></title><description><![CDATA[<p dir="auto">Just want to share some debugging tips for the issue:<br />
Step 1: Put the debug output inside <code>data</code> method of your implementation the <code>QAbstractItemModel</code> class, like</p>
<pre><code>QVariant TestViewModel::data(const QModelIndex &amp;index, int role) const
{
        qDebug() &lt;&lt; "data" &lt;&lt; index.row() &lt;&lt; index.column() &lt;&lt; role;
        ...
}
</code></pre>
<p dir="auto">Then make sure you are updating the cell with same index adding the same output before emiting <code>dataChanged</code> signal.</p>
]]></description><link>https://forum.qt.io/post/837445</link><guid isPermaLink="true">https://forum.qt.io/post/837445</guid><dc:creator><![CDATA[Oleksii_S]]></dc:creator><pubDate>Tue, 31 Mar 2026 08:27:28 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Wed, 20 Sep 2023 18:53:53 GMT]]></title><description><![CDATA[<p dir="auto">Quite the contrary, proper role handling is paramount to have a correctly working model view implementation.</p>
]]></description><link>https://forum.qt.io/post/773304</link><guid isPermaLink="true">https://forum.qt.io/post/773304</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 20 Sep 2023 18:53:53 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Wed, 20 Sep 2023 06:50:21 GMT]]></title><description><![CDATA[<p dir="auto">Yes i do use QML sorry.<br />
So what you're saying is that if you use QML the roles handled on the code side are irrelevant ?<br />
It seems a bit weird as if a edit a values at runtime i do get the proper role in the setData callback.<br />
It just that i've neglected the column of the index because i was thinking that the view won't be needing it as it had the role. And because the setData callback have an index without the column setup when trigged by edition.</p>
]]></description><link>https://forum.qt.io/post/773240</link><guid isPermaLink="true">https://forum.qt.io/post/773240</guid><dc:creator><![CDATA[Coubz]]></dc:creator><pubDate>Wed, 20 Sep 2023 06:50:21 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Tue, 19 Sep 2023 18:34:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/coubz">@<bdi>Coubz</bdi></a> well, once you mention using QML it starts to make sense.</p>
<p dir="auto">QML uses custom roles to access data hence if you don't tell dataChanged that the data corresponding to these roles has changed, then there's no reason for the view to update.</p>
]]></description><link>https://forum.qt.io/post/773195</link><guid isPermaLink="true">https://forum.qt.io/post/773195</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 19 Sep 2023 18:34:13 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Tue, 19 Sep 2023 18:28:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/coubz">@<bdi>Coubz</bdi></a><br />
You posted this topic in <strong>General and Desktop</strong> rather than <a href="https://forum.qt.io/category/12/qml-and-qt-quick">https://forum.qt.io/category/12/qml-and-qt-quick</a>.  I assumed you were talking about a Qt widgets <code>QTreeView</code>.  Are you in fact using QML and its <code>TreeView</code>?  You never mentioned QML....</p>
]]></description><link>https://forum.qt.io/post/773193</link><guid isPermaLink="true">https://forum.qt.io/post/773193</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 19 Sep 2023 18:28:33 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Tue, 19 Sep 2023 09:56:27 GMT]]></title><description><![CDATA[<p dir="auto">I've managed to make it work, i'm not exactly sure to understand why tho.<br />
I had to change the way the setData reacts :</p>
<pre><code>bool MapMarkerTreeModel::setData(const QModelIndex&amp; _index, const QVariant&amp; _value, int _role/* = Qt::EditRole*/)
{
    if (!_index.isValid())
    {
        return false;
    }

    if(MapMarkerTreeItem* item = getItem(_index))
    {
        if(item-&gt;setData(_value, _role))
        {
            //emit dataChanged(_index, _index); // &lt;-----  Previous version

            QList&lt;int&gt; roles{Qt::DisplayRole, Qt::EditRole, _role};
            QModelIndex id = createIndex(_index.row(), item-&gt;getColumnIdFromRole(_role), item);
            emit dataChanged(id, id, roles);
            return true;
        }
    }

    return false;
}
</code></pre>
<p dir="auto">So it seems that the dataChanged signals have to build the role into the column of the ID else it won't refresh properly.<br />
I thought the role param of the signal would be able to do the trick but i think there is something i didn't understood here.</p>
]]></description><link>https://forum.qt.io/post/773142</link><guid isPermaLink="true">https://forum.qt.io/post/773142</guid><dc:creator><![CDATA[Coubz]]></dc:creator><pubDate>Tue, 19 Sep 2023 09:56:27 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Wed, 13 Sep 2023 16:33:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/coubz">@<bdi>Coubz</bdi></a><br />
I agree with @Christian-Ehrlicher.  I would suspect the <code>dataChanged()</code> signal itself is fine and being acted on, but somehow the <em>indexes</em> are not what you think they are so it does not redraw the right thing?</p>
]]></description><link>https://forum.qt.io/post/772621</link><guid isPermaLink="true">https://forum.qt.io/post/772621</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 13 Sep 2023 16:33:59 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Wed, 13 Sep 2023 09:07:46 GMT]]></title><description><![CDATA[<p dir="auto">Your dataChanged() is wrong somehow. Minimize your code, remove qml and use a simple QTreeView until you can either reproduce it in a minimal compilable example or find your bug. The models work correct and don't need a special update except dataChanged() when some data changes inside the model.</p>
]]></description><link>https://forum.qt.io/post/772589</link><guid isPermaLink="true">https://forum.qt.io/post/772589</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 13 Sep 2023 09:07:46 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Wed, 13 Sep 2023 08:59:06 GMT]]></title><description><![CDATA[<p dir="auto">Is it possible that i miss something to set the TreeView as "dirty" ?<br />
The TreeView or an other Qml element that contains it, or the items inside the view maybe.<br />
It's weird because as i said if i drag the view to move the text of the items outside of the windows thay will update.</p>
<p dir="auto">Currently the hierarchy is something like that :</p>
<pre><code>ApplicationWindow
    Rectangle
        GridLayout
            Item
                ColumnLayout
                    Item
                        Rectangle
                            TreeView
                                TreeViewDelegate
                                    Row
                                        CheckBox
                                        Text
</code></pre>
]]></description><link>https://forum.qt.io/post/772584</link><guid isPermaLink="true">https://forum.qt.io/post/772584</guid><dc:creator><![CDATA[Coubz]]></dc:creator><pubDate>Wed, 13 Sep 2023 08:59:06 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Tue, 12 Sep 2023 07:01:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Yep I've tryed that already.<br />
It shown me some issues that i've corrected but the datachanged(...) still doesn't do anything.<br />
One noticeable fix was to return an invalid QModelIndex with the parent method when the parent is the root node of the tree.<br />
Is there a way to be able to break in the TreeView code to follow the signal reception an see what's wrong?</p>
]]></description><link>https://forum.qt.io/post/772434</link><guid isPermaLink="true">https://forum.qt.io/post/772434</guid><dc:creator><![CDATA[Coubz]]></dc:creator><pubDate>Tue, 12 Sep 2023 07:01:19 GMT</pubDate></item><item><title><![CDATA[Reply to TreeView not updating after a dataChanged signal on Sun, 10 Sep 2023 19:00:43 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">One thing you can do is use the <a href="https://doc.qt.io/qt-6/qabstractitemmodeltester.html" target="_blank" rel="noopener noreferrer nofollow ugc">QAbstractItemModelTester</a> class to check that your implementation works as expected.</p>
]]></description><link>https://forum.qt.io/post/772228</link><guid isPermaLink="true">https://forum.qt.io/post/772228</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sun, 10 Sep 2023 19:00:43 GMT</pubDate></item></channel></rss>