<?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[emit dataChanged refreshes QTableView only once.]]></title><description><![CDATA[<p dir="auto">Hi!</p>
<p dir="auto">I'm working on my code actually and I spotted a problem. I have an app that filters files. Every time I add new filter to QTreeView I want to refresh view and add it to Table. So far I created code like this</p>
<pre><code>//constructor

FilterModel::FilterModel(QObject* parent, const std::shared_ptr&lt;FilterManager&gt;&amp; filterManager)
    : QAbstractTableModel(parent), mFilterManager(filterManager)
{
    mFilterManager-&gt;addListener([this](unsigned int id){
        QModelIndex idx1 = index(mFilterManager-&gt;getIndexById(id), NAME_COLUMN);
        QModelIndex idx2 = index(mFilterManager-&gt;getIndexById(id), DESCRIPTION_COLUMN);
        emit dataChanged(idx1, idx2, QVector&lt;int&gt;{Qt::DisplayRole});
    }
    );
}

//Add Listener method

void FilterManager::addListener(std::function&lt;void(unsigned int)&gt; f)
{
    listeners.append(f);
}


//notify method

void FilterManager::notifyListeners(unsigned int id)
{
    for (auto&amp;&amp; listener : listeners)
    {
        listener(id);
    }
}
</code></pre>
<p dir="auto">notifyListeners is triggered every time data changes (eg. filter is added or activity is changed). View refreshes only when I resize window, and do it once in one execution of program.</p>
<p dir="auto">I'm using 5.2.1, on Ubuntu.</p>
<p dir="auto">Thanks.</p>
]]></description><link>https://forum.qt.io/topic/93293/emit-datachanged-refreshes-qtableview-only-once</link><generator>RSS for Node</generator><lastBuildDate>Sun, 03 May 2026 21:33:08 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/93293.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 02 Aug 2018 08:05:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to emit dataChanged refreshes QTableView only once. on Thu, 02 Aug 2018 08:54:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a><br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a><br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">Nice! Thanks a lot!</p>
]]></description><link>https://forum.qt.io/post/473453</link><guid isPermaLink="true">https://forum.qt.io/post/473453</guid><dc:creator><![CDATA[sweetMango]]></dc:creator><pubDate>Thu, 02 Aug 2018 08:54:39 GMT</pubDate></item><item><title><![CDATA[Reply to emit dataChanged refreshes QTableView only once. on Thu, 02 Aug 2018 08:52:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sweetmango">@<bdi>sweetMango</bdi></a><br />
As <a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> has just beaten me to posting: when changing the <em>model</em>:</p>
<ul>
<li>If data in an <em>existing</em> row has changed, you need to emit <code>dataChanged</code>.</li>
<li>But if a new row has been inserted or an existing row deleted you need to call <code>begin/endInsert/DeleteRows()</code>.</li>
</ul>
<p dir="auto">The view needs to know differently from <code>dataChanged</code> when the number of rows changes.</p>
]]></description><link>https://forum.qt.io/post/473452</link><guid isPermaLink="true">https://forum.qt.io/post/473452</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 02 Aug 2018 08:52:16 GMT</pubDate></item><item><title><![CDATA[Reply to emit dataChanged refreshes QTableView only once. on Thu, 02 Aug 2018 08:48:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sweetmango">@<bdi>sweetMango</bdi></a> said in <a href="/post/473445">emit dataChanged refreshes QTableView only once.</a>:</p>
<blockquote>
<p dir="auto">don't exist in View, but after refresh I want it to pop up in<br />
QTableView.</p>
</blockquote>
<p dir="auto">Then <code>dataChanged</code> is wrong. You need to use <code>beginInsertRows()</code> and <code>endInsertRows()</code> instead.</p>
]]></description><link>https://forum.qt.io/post/473450</link><guid isPermaLink="true">https://forum.qt.io/post/473450</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Thu, 02 Aug 2018 08:48:32 GMT</pubDate></item><item><title><![CDATA[Reply to emit dataChanged refreshes QTableView only once. on Thu, 02 Aug 2018 08:49:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sweetmango">@<bdi>sweetMango</bdi></a> said in <a href="/post/473445">emit dataChanged refreshes QTableView only once.</a>:</p>
<blockquote>
<p dir="auto">So before refresh it don't exist in View, but after refresh I want it to pop up in</p>
</blockquote>
<p dir="auto">Ok, this is the key of the problem.</p>
<p dir="auto"><code>dataChanged</code> only refreshes the contents of <strong>existing</strong> indexes. You need to use <code>beginInsertRows</code>/<code>endInsertRows</code> to notify the view of new rows spawning</p>
<p dir="auto">Edit:<br />
sorry for the double ninja <a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a></p>
]]></description><link>https://forum.qt.io/post/473449</link><guid isPermaLink="true">https://forum.qt.io/post/473449</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 02 Aug 2018 08:49:11 GMT</pubDate></item><item><title><![CDATA[Reply to emit dataChanged refreshes QTableView only once. on Thu, 02 Aug 2018 08:41:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a><br />
I've applied your suggestions, and every time I add filter, console shows "changed", so signal is emitted, but view refreshes only once in lifetime of program.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a><br />
Yes, every time I change something, I'm calling notifyListeners. I'm pretty sure, that ids are correct. The goal is to refresh only one row in view, with specific filter. So before refresh it don't exist in View, but after refresh I want it to pop up in<br />
QTableView.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
Still thanks for your reply!</p>
]]></description><link>https://forum.qt.io/post/473445</link><guid isPermaLink="true">https://forum.qt.io/post/473445</guid><dc:creator><![CDATA[sweetMango]]></dc:creator><pubDate>Thu, 02 Aug 2018 08:41:35 GMT</pubDate></item><item><title><![CDATA[Reply to emit dataChanged refreshes QTableView only once. on Thu, 02 Aug 2018 08:26:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sweetmango">@<bdi>sweetMango</bdi></a><br />
I'm not sure what you're saying you do/do not want, or what you are/are not seeing, but I think the view only refreshes when it next hits the event loop and gets time to repaint, no matter how many times it might have received &amp; acted on multiple <code>dataChanged</code> signals.  If you want it to update immediately on each one, I think you need to call <a href="http://doc.qt.io/qt-5/qwidget.html#repaint" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qwidget.html#repaint</a>.  But maybe that's not what you mean....</p>
<p dir="auto"><strong>EDIT</strong> Looks like <a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> understands what you're saying better than I do, sorry!</p>
]]></description><link>https://forum.qt.io/post/473438</link><guid isPermaLink="true">https://forum.qt.io/post/473438</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 02 Aug 2018 08:26:35 GMT</pubDate></item><item><title><![CDATA[Reply to emit dataChanged refreshes QTableView only once. on Thu, 02 Aug 2018 08:23:47 GMT]]></title><description><![CDATA[<p dir="auto">So each time you update something, you are calling your listener functions, right?</p>
<p dir="auto">Are you sure you are passing valid <strong>id</strong>s to them? Are you sure the constructed <em>idx1</em> and <em>idx2</em> are valid and correct?</p>
<p dir="auto">Are you sure dataChanged() is really being emitted? Because you are trying to emit it from a different object (FilterManager instead of FilterModel)?</p>
]]></description><link>https://forum.qt.io/post/473437</link><guid isPermaLink="true">https://forum.qt.io/post/473437</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Thu, 02 Aug 2018 08:23:47 GMT</pubDate></item><item><title><![CDATA[Reply to emit dataChanged refreshes QTableView only once. on Thu, 02 Aug 2018 08:25:26 GMT]]></title><description><![CDATA[<p dir="auto">can you add:</p>
<ul>
<li><code>connect(this,&amp;FilterModel::dataChanged,[](){qDebug("changed");});</code> to  <code>FilterModel::FilterModel</code> to make sure the signal is emitted</li>
<li><code>Q_ASSERT(idx1.isValid()); Q_ASSERT(idx2 .isValid());</code> to the lambda inside <code>addListener</code> to make sure you are passing valid indexes to the view</li>
</ul>
]]></description><link>https://forum.qt.io/post/473436</link><guid isPermaLink="true">https://forum.qt.io/post/473436</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 02 Aug 2018 08:25:26 GMT</pubDate></item></channel></rss>