<?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[QModelIndex::constInternalPointer returns nullptr unexpectedly]]></title><description><![CDATA[<ul>
<li>
<p dir="auto">code</p>
<pre><code class="language-cpp">QModelIndex MyModel::index(int row, int column, const QModelIndex &amp; parent) const
{
    if (parent.isValid())
    {
        const void * p_parent_data = parent.constInternalPointer();
        assert(p_parent_data != nullptr);
        ...
        assert(p != nullptr);
        return createIndex(row, column, p);
    }
    else
    {
        ...
        assert(p != nullptr);
        return createIndex(row, column, p);
    }
}
</code></pre>
</li>
<li>
<p dir="auto">Question<br />
<code>QModelIndex::constInternalPointer</code> in return value of <code>MyModel::index</code> is always non-null, how could <code>assert(p_parent_data != nullptr)</code> in the above codes fail?</p>
</li>
</ul>
]]></description><link>https://forum.qt.io/topic/131429/qmodelindex-constinternalpointer-returns-nullptr-unexpectedly</link><generator>RSS for Node</generator><lastBuildDate>Sun, 07 Jun 2026 09:42:54 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/131429.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 26 Oct 2021 06:23:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QModelIndex::constInternalPointer returns nullptr unexpectedly on Tue, 26 Oct 2021 07:19:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jronald">@<bdi>jronald</bdi></a> said in <a href="/post/686723">QModelIndex::constInternalPointer returns nullptr unexpectedly</a>:</p>
<blockquote>
<p dir="auto">how could <code>assert(parent)</code> in the above codes fail?</p>
</blockquote>
<p dir="auto">"It can't!"</p>
<p dir="auto">How do you <em>know</em> <code>void * parent</code> is non-null?  If you are looking at it in debugger watch window, don't rely on that, it could be being held in a register and the value of <code>parent</code> be unreliable.  Print out the value of <code>parent</code> on the line before the <code>assert()</code>.</p>
<p dir="auto">Try making your <code>void * parent</code> be <code>const void * parent</code> (I'm surprised you don't get a compiler warning)?</p>
<p dir="auto">Meanwhile I find your</p>
<pre><code>void * parent = parent.constInternalPointer();
</code></pre>
<p dir="auto">"strange".  The right-hand side uses the <code>const QModelIndex &amp; parent</code> while the left-hand side uses the new <code>void * parent</code> declaration.  This is at best "confusing".  Suggest rename the local pointer variable to something else.</p>
<p dir="auto">Finally, are you sure you are compiling for debug?  Else <code>assert()</code> is a no-op....</p>
]]></description><link>https://forum.qt.io/post/686744</link><guid isPermaLink="true">https://forum.qt.io/post/686744</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 26 Oct 2021 07:19:26 GMT</pubDate></item><item><title><![CDATA[Reply to QModelIndex::constInternalPointer returns nullptr unexpectedly on Tue, 26 Oct 2021 17:49:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/686744">QModelIndex::constInternalPointer returns nullptr unexpectedly</a>:</p>
<blockquote>
<p dir="auto">"It can't!"</p>
</blockquote>
<p dir="auto">Alright</p>
<p dir="auto">The problem for me is that not only each <code>QModelIndex</code> in the first column may have parent<br />
but each <code>QModelIndex</code> in the remaining columns should also have the same parent as the <code>QModelIndex</code> in the first column of the same row.</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.qt.io/post/686866</link><guid isPermaLink="true">https://forum.qt.io/post/686866</guid><dc:creator><![CDATA[jronald]]></dc:creator><pubDate>Tue, 26 Oct 2021 17:49:39 GMT</pubDate></item><item><title><![CDATA[Reply to QModelIndex::constInternalPointer returns nullptr unexpectedly on Tue, 26 Oct 2021 08:16:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
I've refined the code.</p>
<p dir="auto">Run in debug mode, and <code>assert(p_parent_data != nullptr)</code> fails.</p>
<p dir="auto">The problem might be in <code>QModelIndex MyModel::parent(const QModelIndex &amp; child) const</code><br />
because there are 3 columns and only the first column might have children, for other columns <code>MyModel::parent</code> always returns <code>QModelIndex()</code></p>
]]></description><link>https://forum.qt.io/post/686754</link><guid isPermaLink="true">https://forum.qt.io/post/686754</guid><dc:creator><![CDATA[jronald]]></dc:creator><pubDate>Tue, 26 Oct 2021 08:16:16 GMT</pubDate></item><item><title><![CDATA[Reply to QModelIndex::constInternalPointer returns nullptr unexpectedly on Tue, 26 Oct 2021 07:19:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jronald">@<bdi>jronald</bdi></a> said in <a href="/post/686723">QModelIndex::constInternalPointer returns nullptr unexpectedly</a>:</p>
<blockquote>
<p dir="auto">how could <code>assert(parent)</code> in the above codes fail?</p>
</blockquote>
<p dir="auto">"It can't!"</p>
<p dir="auto">How do you <em>know</em> <code>void * parent</code> is non-null?  If you are looking at it in debugger watch window, don't rely on that, it could be being held in a register and the value of <code>parent</code> be unreliable.  Print out the value of <code>parent</code> on the line before the <code>assert()</code>.</p>
<p dir="auto">Try making your <code>void * parent</code> be <code>const void * parent</code> (I'm surprised you don't get a compiler warning)?</p>
<p dir="auto">Meanwhile I find your</p>
<pre><code>void * parent = parent.constInternalPointer();
</code></pre>
<p dir="auto">"strange".  The right-hand side uses the <code>const QModelIndex &amp; parent</code> while the left-hand side uses the new <code>void * parent</code> declaration.  This is at best "confusing".  Suggest rename the local pointer variable to something else.</p>
<p dir="auto">Finally, are you sure you are compiling for debug?  Else <code>assert()</code> is a no-op....</p>
]]></description><link>https://forum.qt.io/post/686744</link><guid isPermaLink="true">https://forum.qt.io/post/686744</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 26 Oct 2021 07:19:26 GMT</pubDate></item></channel></rss>