<?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[QTreeView rowCount() value differs]]></title><description><![CDATA[<p dir="auto">Hi All,<br />
I have QTreeView ,with QFileSystemModel as its model.</p>
<pre><code>model = new QFileSystemModel(this);
model-&gt;setRootPath("");
ui-&gt;treeView-&gt;setRootIndex(model-&gt;index(""));
</code></pre>
<p dir="auto">I have connected QTreeView expanded signal with a slot in my Class,</p>
<pre><code>connect(ui-&gt;treeView,SIGNAL(expanded(const QModelIndex&amp;)),this,SLOT(expand(const QModelIndex&amp;)));
</code></pre>
<p dir="auto">When i try expand my <strong>MEDIA</strong> Drive which has 3 folders,  again and again, my values differ.</p>
<p dir="auto"><img src="https://cdn1.imggmi.com/uploads/2018/9/11/a9eefd62c2d5082a83730555b86cdb34-full.png" alt="" class=" img-fluid img-markdown" /></p>
<p dir="auto">My Slot function is  as follow,</p>
<pre><code>void MainWindow::expand(const QModelIndex &amp;index)
{
    qDebug() &lt;&lt; model-&gt;data(index,Qt::DisplayRole);
    qDebug() &lt;&lt; model-&gt;hasChildren(index);
    qDebug() &lt;&lt; model-&gt;hasIndex(0,0,index);
    qDebug() &lt;&lt; model-&gt;rowCount(index) ;
}
</code></pre>
<p dir="auto">during first expansion, the values of <strong>rowCount()</strong> is 0</p>
<p dir="auto"><em>QVariant(QString, "MEDIA (F:)")<br />
true<br />
false<br />
0</em></p>
<p dir="auto">for the subsequent expansion, value of <strong>rowCount()</strong> is 3</p>
<p dir="auto"><em>QVariant(QString, "MEDIA (F:)")<br />
true<br />
true<br />
3</em></p>
<p dir="auto">I would like to know why <strong>rowCount()</strong> and <strong>hadIndex()</strong> value differs , is it a bug?? or am i missing something ...</p>
<p dir="auto"><strong>Qt 5.9.1 , Windows 10</strong> is used.</p>
<p dir="auto">Any Help is deeply appreciated...</p>
<p dir="auto">Thank You!</p>
]]></description><link>https://forum.qt.io/topic/94506/qtreeview-rowcount-value-differs</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Jul 2026 18:42:43 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/94506.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 11 Sep 2018 15:27:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Fri, 14 Sep 2018 03:12:26 GMT]]></title><description><![CDATA[<p dir="auto">Thanks <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> , <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>  ,  <a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> for your suggestions...</p>
]]></description><link>https://forum.qt.io/post/481324</link><guid isPermaLink="true">https://forum.qt.io/post/481324</guid><dc:creator><![CDATA[Vinoth Rajendran4]]></dc:creator><pubDate>Fri, 14 Sep 2018 03:12:26 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 17:47:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vinoth-rajendran4">@<bdi>Vinoth-Rajendran4</bdi></a><br />
I imagine that the first <code>directoryLoaded</code> corresponds to separate thread actually reading from the OS file system.  That takes "a bit of time".  But subsequent <code>directoryLoaded</code> signals just correspond to  <code>QFileSystemModel </code> looking up in its cache and seeing that directory has already been loaded.  Hence the variation in the timing of when the signals arrive.  Something like that.</p>
]]></description><link>https://forum.qt.io/post/481264</link><guid isPermaLink="true">https://forum.qt.io/post/481264</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 13 Sep 2018 17:47:49 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 17:36:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vinoth-rajendran4">@<bdi>Vinoth-Rajendran4</bdi></a>  That sounds exactly what I was anticipating....</p>
]]></description><link>https://forum.qt.io/post/481263</link><guid isPermaLink="true">https://forum.qt.io/post/481263</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 13 Sep 2018 17:36:53 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 17:17:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> :  Seems, during the very first time expansion of a  directory , slot of <strong>expanded()</strong> signal is called ahead of <strong>directoryLoaded()</strong> , hence the <em>rowCount()</em> is 0.<br />
But during subsequent expansion of the same directory,  slot of  <strong>directoryLoaded()</strong> is called ahead of  <strong>expanded()</strong> signal , hence now i am receiving correct <em>rowCount()</em> value.</p>
<p dir="auto">Seems a little code alternation might do the trick.</p>
]]></description><link>https://forum.qt.io/post/481260</link><guid isPermaLink="true">https://forum.qt.io/post/481260</guid><dc:creator><![CDATA[Vinoth Rajendran4]]></dc:creator><pubDate>Thu, 13 Sep 2018 17:17:25 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 16:49:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vinoth-rajendran4">@<bdi>Vinoth-Rajendran4</bdi></a><br />
That link includes:</p>
<blockquote>
<p dir="auto">Unlike QDirModel, QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory.</p>
</blockquote>
<p dir="auto">It says it's using a separate thread.  You said you tried my suggestion of making the debug calls to the functions not start till a second after the expansion and that <em>did</em> make them return the correct answers.  If you put that together with the separate thread....</p>
<p dir="auto">I think you should try putting a slot on the <a href="http://doc.qt.io/qt-5/qfilesystemmodel.html#directoryLoaded" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qfilesystemmodel.html#directoryLoaded</a> signal.  Let's see it report on which directories it finished loading when?  Compare when those signals arrive to the value of your <code>rowCount()</code> etc.</p>
<p dir="auto">If that indeed explains the behaviour, you may have to rewrite your code a bit so that you use that signal in combination with the <code>expanded</code> one so that you can get your results <em>reliably</em>?</p>
]]></description><link>https://forum.qt.io/post/481255</link><guid isPermaLink="true">https://forum.qt.io/post/481255</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 13 Sep 2018 16:49:22 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 16:39:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> :  the reason why rowcount() is 0 , might be justified with this link , <a href="http://doc.qt.io/qt-5/qfilesystemmodel.html#caching-and-performance" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qfilesystemmodel.html#caching-and-performance</a> .</p>
<p dir="auto">Not sure though. Can you please provide your insight .</p>
]]></description><link>https://forum.qt.io/post/481253</link><guid isPermaLink="true">https://forum.qt.io/post/481253</guid><dc:creator><![CDATA[Vinoth Rajendran4]]></dc:creator><pubDate>Thu, 13 Sep 2018 16:39:27 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 11:55:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> : my entire code below,</p>
<pre><code>MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    model = new QFileSystemModel;
    model-&gt;setRootPath("");

    ui-&gt;treeView-&gt;setModel(model);
    ui-&gt;treeView-&gt;setRootIndex(model-&gt;index(""));
    connect(ui-&gt;treeView,SIGNAL(expanded(QModelIndex)),this,SLOT(func(QModelIndex)));
}

void MainWindow::func(QModelIndex index)
{
      qDebug() &lt;&lt; model-&gt;data(index1,Qt::DisplayRole);
     qDebug() &lt;&lt; model-&gt;hasChildren(index1);
     qDebug() &lt;&lt; model-&gt;hasIndex(0,0,index1);
     qDebug() &lt;&lt; model-&gt;rowCount(index1);
}
</code></pre>
]]></description><link>https://forum.qt.io/post/481171</link><guid isPermaLink="true">https://forum.qt.io/post/481171</guid><dc:creator><![CDATA[Vinoth Rajendran4]]></dc:creator><pubDate>Thu, 13 Sep 2018 11:55:38 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 11:44:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> : Media is System disk</p>
]]></description><link>https://forum.qt.io/post/481170</link><guid isPermaLink="true">https://forum.qt.io/post/481170</guid><dc:creator><![CDATA[Vinoth Rajendran4]]></dc:creator><pubDate>Thu, 13 Sep 2018 11:44:27 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 08:31:39 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/481064">QTreeView rowCount() value differs</a>:</p>
<blockquote>
<p dir="auto">Is Media a system disk or an external storage ?</p>
</blockquote>
<p dir="auto">I wondered about this (slow external media gets updated "slowly" under Windows).   That's why I asked the OP when it happens</p>
<blockquote>
<p dir="auto">Does this behaviour happen on any directory node in the tree, or only certain ones (like, only volume ones)?</p>
</blockquote>
<p dir="auto">but he says</p>
<blockquote>
<p dir="auto">It happens on every directory node in the tree...</p>
</blockquote>
<p dir="auto">so assuming that's true it's not to do with media type.</p>
]]></description><link>https://forum.qt.io/post/481134</link><guid isPermaLink="true">https://forum.qt.io/post/481134</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 13 Sep 2018 08:31:39 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Thu, 13 Sep 2018 07:47:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vinoth-rajendran4">@<bdi>Vinoth-Rajendran4</bdi></a> said in <a href="/post/481008">QTreeView rowCount() value differs</a>:</p>
<blockquote>
<p dir="auto">Can this be a bug ???</p>
</blockquote>
<p dir="auto">Can you provide a minimum example to replicate the problem? if we can confirm the bug we cat take a look if it's solvable</p>
]]></description><link>https://forum.qt.io/post/481121</link><guid isPermaLink="true">https://forum.qt.io/post/481121</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 13 Sep 2018 07:47:58 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Wed, 12 Sep 2018 21:30:07 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Is Media a system disk or an external storage ?</p>
]]></description><link>https://forum.qt.io/post/481064</link><guid isPermaLink="true">https://forum.qt.io/post/481064</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 12 Sep 2018 21:30:07 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Wed, 12 Sep 2018 14:52:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vinoth-rajendran4">@<bdi>Vinoth-Rajendran4</bdi></a></p>
<blockquote>
<p dir="auto">your workaround suggestion with single-shot timer , works fine.</p>
</blockquote>
<p dir="auto">So, let's be clear.  You only get (normally) get a count of 0 first time.  If you make it so the first time you examine the children is a second after the expansion was performed it does produce the correct count.  Is that what you are saying?</p>
<p dir="auto">If it happens on every directory, my impression is there would be code out there which simply does not work.  It's like your <code>expand()</code> is being called at the very start of the expansion, but it's the <code>expanded</code> slot and that should be called after the expand has been acted on, I assume.</p>
]]></description><link>https://forum.qt.io/post/481020</link><guid isPermaLink="true">https://forum.qt.io/post/481020</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 12 Sep 2018 14:52:13 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Wed, 12 Sep 2018 14:30:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> : your workaround suggestion with single-shot timer ,  works fine.</p>
<pre><code>Does this behaviour happen on any directory node in the tree, or only certain ones (like, only volume ones)?
</code></pre>
<p dir="auto">It happens on every directory node in the tree...</p>
]]></description><link>https://forum.qt.io/post/481016</link><guid isPermaLink="true">https://forum.qt.io/post/481016</guid><dc:creator><![CDATA[Vinoth Rajendran4]]></dc:creator><pubDate>Wed, 12 Sep 2018 14:30:03 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Wed, 12 Sep 2018 14:05:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vinoth-rajendran4">@<bdi>Vinoth-Rajendran4</bdi></a></p>
<blockquote>
<p dir="auto">Can this be a bug ???</p>
</blockquote>
<p dir="auto">We don't know yet.  Does it matter?  At this point I assume you're looking for a workaround.</p>
<p dir="auto">Given that <a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a>'s suggestions have not worked, at this point if it were me: I would test your code which outputs the numbers by calling it from your <code>expand()</code> <em>via a single-shot <code>QTimer</code></em> with a delay of, say, a second.  Let's see whether by any chance there's some OS activity going on which does not properly expand a top-level volume directory immediately and only really does it asynchronously?  Does this behaviour happen on any directory node in the tree, or only certain ones (like, only volume ones)?</p>
]]></description><link>https://forum.qt.io/post/481013</link><guid isPermaLink="true">https://forum.qt.io/post/481013</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 12 Sep 2018 14:05:47 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Wed, 12 Sep 2018 13:33:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> , with Qt::QueuedConnection also, issue is seen.</p>
<p dir="auto">Can this be a bug ???</p>
]]></description><link>https://forum.qt.io/post/481008</link><guid isPermaLink="true">https://forum.qt.io/post/481008</guid><dc:creator><![CDATA[Vinoth Rajendran4]]></dc:creator><pubDate>Wed, 12 Sep 2018 13:33:21 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Wed, 12 Sep 2018 06:50:18 GMT]]></title><description><![CDATA[<p dir="auto">Can you try adding <code>Qt::QueuedConnection</code> as the last argument to your connect and see if it works then?</p>
]]></description><link>https://forum.qt.io/post/480949</link><guid isPermaLink="true">https://forum.qt.io/post/480949</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Wed, 12 Sep 2018 06:50:18 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Wed, 12 Sep 2018 02:10:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a>  : Tried your approach, still issue is seen.</p>
<pre><code>model-&gt;canFetchMore(index)
</code></pre>
<p dir="auto">return false always....</p>
<p dir="auto">Is there any specific reason why model not reading  its row contents during the very first expansion ???</p>
<p dir="auto">Thank You!</p>
]]></description><link>https://forum.qt.io/post/480919</link><guid isPermaLink="true">https://forum.qt.io/post/480919</guid><dc:creator><![CDATA[Vinoth Rajendran4]]></dc:creator><pubDate>Wed, 12 Sep 2018 02:10:04 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Tue, 11 Sep 2018 18:19:57 GMT]]></title><description><![CDATA[<p dir="auto">I'm not 100% sure but I think it has to do with lazy loading (<code>QFileSystemModel::fetchMore</code>). When you expand the first time the model did not actually "read" those rows yet.</p>
<p dir="auto">If this is the case you can use</p>
<pre><code class="language-cpp">void MainWindow::expand(const QModelIndex &amp;index)
{
while(model-&gt;canFetchMore(index))
model-&gt;fetchMore(index);
    qDebug() &lt;&lt; model-&gt;data(index,Qt::DisplayRole);
    qDebug() &lt;&lt; model-&gt;hasChildren(index);
    qDebug() &lt;&lt; model-&gt;hasIndex(0,0,index);
    qDebug() &lt;&lt; model-&gt;rowCount(index) ;
}
</code></pre>
]]></description><link>https://forum.qt.io/post/480866</link><guid isPermaLink="true">https://forum.qt.io/post/480866</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Tue, 11 Sep 2018 18:19:57 GMT</pubDate></item><item><title><![CDATA[Reply to QTreeView rowCount() value differs on Tue, 11 Sep 2018 16:46:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vinoth-rajendran4">@<bdi>Vinoth-Rajendran4</bdi></a><br />
To be clear: when it returns 0 first time, how do we know from your code just which node is being expanded?  Could you debug out the <code>index</code> or the label of the item or similar?</p>
]]></description><link>https://forum.qt.io/post/480861</link><guid isPermaLink="true">https://forum.qt.io/post/480861</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 11 Sep 2018 16:46:30 GMT</pubDate></item></channel></rss>