<?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[qabstractitemmodeltester and editabletreemodel test failures]]></title><description><![CDATA[<p dir="auto">In qabstractitemmodeltester, the following test case is run when used on a tree model.<br />
<strong>qabstractitemmodeltester  - SNIPPET 1:</strong></p>
<pre><code>// Common error test #3, the second column should NOT have the same children
  // as the first column in a row.
  // Usually the second column shouldn't have children.
  if (model-&gt;hasIndex(0, 1)) {
      QModelIndex topIndex1 = model-&gt;index(0, 1, QModelIndex());
      MODELTESTER_VERIFY(topIndex1.isValid());
      if (model-&gt;hasChildren(topIndex) &amp;&amp; model-&gt;hasChildren(topIndex1)) {
          QModelIndex childIndex = model-&gt;index(0, 0, topIndex);
          MODELTESTER_VERIFY(childIndex.isValid());
          QModelIndex childIndex1 = model-&gt;index(0, 0, topIndex1);
          MODELTESTER_VERIFY(childIndex1.isValid());
          MODELTESTER_VERIFY(childIndex != childIndex1);
      }
  }
</code></pre>
<p dir="auto">As per the editableTreeModel example, if the column is anything other than 0, the model returns an invalid index in the index method as shown below:<br />
<strong>editableTreeModel  - SNIPPET 2:</strong></p>
<pre><code>if (parent.isValid() &amp;&amp; parent.column() != 0)
      return QModelIndex();
</code></pre>
<p dir="auto"><strong>Going back to the tester code above:</strong><br />
In SNIPPET 1, childIndex1 will be invalid, because SNIPPET 2 returns an invalid index for all columns other than zero.<br />
If I remove the column 0 check in SNIPPET 2, then in SNIPPET 1 it fails on the last line because childIndex IS EQUAL TO childIndex.</p>
<p dir="auto"><strong>What is the right implementation here?  Only when I return an index for all columns, the code seems to work correctly, but the model tester fails.</strong>.</p>
]]></description><link>https://forum.qt.io/topic/118528/qabstractitemmodeltester-and-editabletreemodel-test-failures</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 20:53:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/118528.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 28 Aug 2020 22:38:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to qabstractitemmodeltester and editabletreemodel test failures on Thu, 28 Dec 2023 15:55:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/starterkit">@<bdi>StarterKit</bdi></a> Yes, that "parent.column() &gt; 0" check is necessary in the rowCount method.  That is how I implemented it in all my TreeModels.</p>
<p dir="auto">I do:<br />
if(parent.isValid() &amp;&amp; parent.column() &gt; 0) return 0;</p>
<p dir="auto">Hope that helps.</p>
]]></description><link>https://forum.qt.io/post/784489</link><guid isPermaLink="true">https://forum.qt.io/post/784489</guid><dc:creator><![CDATA[JohnGa]]></dc:creator><pubDate>Thu, 28 Dec 2023 15:55:29 GMT</pubDate></item><item><title><![CDATA[Reply to qabstractitemmodeltester and editabletreemodel test failures on Thu, 28 Dec 2023 13:33:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> , I think I found how it works in the example. There is an additional restriction in <code>rowCount()</code> method of the model:</p>
<pre><code>int TreeModel::rowCount(const QModelIndex &amp;parent) const
{
    if (parent.column() &gt; 0)
        return 0; 
</code></pre>
<p dir="auto">I.e. it simply reports no rows if asked for any column other than the left one. As result I assume there are no queries done for child indices outside the left column.</p>
<p dir="auto">Probably it is right... but not obvious...</p>
]]></description><link>https://forum.qt.io/post/784481</link><guid isPermaLink="true">https://forum.qt.io/post/784481</guid><dc:creator><![CDATA[StarterKit]]></dc:creator><pubDate>Thu, 28 Dec 2023 13:33:41 GMT</pubDate></item><item><title><![CDATA[Reply to qabstractitemmodeltester and editabletreemodel test failures on Thu, 28 Dec 2023 11:53:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/davidfaure">@<bdi>DavidFaure</bdi></a> knows maybe more on what's wrong here.</p>
]]></description><link>https://forum.qt.io/post/784477</link><guid isPermaLink="true">https://forum.qt.io/post/784477</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Thu, 28 Dec 2023 11:53:41 GMT</pubDate></item><item><title><![CDATA[Reply to qabstractitemmodeltester and editabletreemodel test failures on Thu, 28 Dec 2023 10:37:43 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> ,<br />
The topic is old, but my question exactly match the context of it so I decided to ask it here instead of creating a new topic.</p>
<p dir="auto">JohnGa gave a snippet of test case and my model fails at the last line:</p>
<pre><code>MODELTESTER_VERIFY(childIndex != childIndex1);
</code></pre>
<p dir="auto">I made it based on <a href="https://doc.qt.io/qt-6/qtwidgets-itemviews-simpletreemodel-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">this example</a> that hase following implementation of <code>index()</code> method:</p>
<pre><code>QModelIndex TreeModel::index(int row, int column, const QModelIndex &amp;parent) const
{
    if (!hasIndex(row, column, parent))
        return QModelIndex();

    TreeItem *parentItem;

    if (!parent.isValid())
        parentItem = rootItem;
    else
        parentItem = static_cast&lt;TreeItem*&gt;(parent.internalPointer());

    TreeItem *childItem = parentItem-&gt;child(row);
    if (childItem)
        return createIndex(row, column, childItem);
    return QModelIndex();
}
</code></pre>
<p dir="auto">As I see the test do the following:<br />
Creates first top index <code>model-&gt;index(0, 0, QModelIndex())</code><br />
Creates second top index <code>model-&gt;index(0, 1, QModelIndex())</code><br />
So three are 2 indices (0, 0, &lt;void&gt;) and (0, 1, &lt;void) but both have the same value of <code>internalPointer</code> as <code>childItem</code> depends on <code>row</code> only.<br />
As next step the test makes calls<br />
<code>model-&gt;index(0, 0, topIndex)</code> and <code>model-&gt;index(0, 0, topIndex1)</code> with different parent indices, but - both of them have the same internal pointer. So the call <code>parentItem-&gt;child(row)</code> will provide the same value of the <code>childItem</code>.<br />
And finally this <code>return createIndex(row, column, childItem)</code> will give two identical indices <code>(0, 0, childItem)</code> that will fail the test.</p>
<p dir="auto">But I don't fully understand what is wrong with all this story.<br />
My guess is that <code>index()</code> method should add <code>childItem</code> data only for column 0. But then <code>parent()</code> method of the example will fail. So I assume it should be changed to be more inteligente looking into <code>internalPointer()</code> of the first column. Is it right understanding or I miss something here?</p>
]]></description><link>https://forum.qt.io/post/784470</link><guid isPermaLink="true">https://forum.qt.io/post/784470</guid><dc:creator><![CDATA[StarterKit]]></dc:creator><pubDate>Thu, 28 Dec 2023 10:37:43 GMT</pubDate></item><item><title><![CDATA[Reply to qabstractitemmodeltester and editabletreemodel test failures on Sun, 30 Aug 2020 08:23:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/johnga">@<bdi>JohnGa</bdi></a> Thx, the columnCount() function is wrong (as already guessed in my first post). It must be<br />
<s>int TreeModel::columnCount(const QModelIndex &amp;parent) const<br />
{<br />
return parent.isValid() ? 0 : rootItem-&gt;columnCount();</s><br />
}</p>
<pre><code>int TreeModel::columnCount(const QModelIndex &amp;parent) const
{
    return (!parent.isValid() || parent.column() == 0) ? rootItem-&gt;columnCount() : 0;
}
</code></pre>
<p dir="auto">-&gt; only the first column has sub-columns, not the second (which would look somehow strange)</p>
<p dir="auto">It has no real-world impact in the Qt since the current implementation does first check for other things but is wrong when you want to catch all cases - the model tester is very picky here :). Will create a patch for it, thx.</p>
]]></description><link>https://forum.qt.io/post/615006</link><guid isPermaLink="true">https://forum.qt.io/post/615006</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sun, 30 Aug 2020 08:23:24 GMT</pubDate></item><item><title><![CDATA[Reply to qabstractitemmodeltester and editabletreemodel test failures on Sun, 30 Aug 2020 01:11:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> Would it be possible for you to look at the editabletreemodel example that ships with QtCreator?  When the model tester is applied to the example, it will fail with the failures above.</p>
<p dir="auto">Here are the steps to apply the modeltester to the editabletreemodel example:<br />
1)after the editabletreemodel example has been setup in QtCreator, open the <a href="http://editabletreemodel.pro" target="_blank" rel="noopener noreferrer nofollow ugc">editabletreemodel.pro</a> file.    Add "testlib" to the this line "QT += widgets ".<br />
2)Open mainwindow.cpp and add ```<br />
"#include &lt;QAbstractItemModelTester&gt;"<br />
3) Add the line shown below</p>
<pre><code>//The line below is in the example
TreeModel *model = new TreeModel(headers, file.readAll());
//Add the line below
new QAbstractItemModelTester(model, QAbstractItemModelTester::FailureReportingMode::Fatal, this);
</code></pre>
<p dir="auto">4)Run the example and you will see the above failures.</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.qt.io/post/614996</link><guid isPermaLink="true">https://forum.qt.io/post/614996</guid><dc:creator><![CDATA[JohnGa]]></dc:creator><pubDate>Sun, 30 Aug 2020 01:11:05 GMT</pubDate></item><item><title><![CDATA[Reply to qabstractitemmodeltester and editabletreemodel test failures on Sat, 29 Aug 2020 08:16:57 GMT]]></title><description><![CDATA[<p dir="auto">So what's your actual implementation? The tester is correct. If the model says it has an index at index(x/y) then it should return a unique and valid one.</p>
]]></description><link>https://forum.qt.io/post/614937</link><guid isPermaLink="true">https://forum.qt.io/post/614937</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Sat, 29 Aug 2020 08:16:57 GMT</pubDate></item></channel></rss>