<?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[Unexpected QList&lt;QVariant&gt; Initialiser Behaviour]]></title><description><![CDATA[<p dir="auto">This isn't so much a problem report, because a solution is easy to implement, but this behaviour is very unexpected and has caused me some wasted time. I post it in case it might help others to avoid it.</p>
<p dir="auto">Initialising a member variable of type</p>
<pre><code>QList&lt;QVariant&gt; results;
</code></pre>
<p dir="auto">using "{}" braces produces the unexpected (to me) result of creating a value of type QList&lt;QList&lt;QVariant&gt;&gt;.<br />
Whereas initialising the member variable using "()" round brackets produces a value of the expected (to me) type QList&lt;QVariant&gt;.<br />
Example code that reproduces this:</p>
<pre><code>class A
{
public:
  A(QList&lt;QVariant&gt; resultsList)
    : results{ resultsList } // &lt;&lt;&lt;&lt;&lt;&lt;
  {
     qDebug() &lt;&lt; "A results are" &lt;&lt; results.size() &lt;&lt; "long";
  }
  QList&lt;QVariant&gt; results;
};

class B
{
public:
  B(QList&lt;QVariant&gt; resultsList)
    : results(resultsList) // &lt;&lt;&lt;&lt;&lt;&lt;
  {
     qDebug() &lt;&lt; "B results are" &lt;&lt; results.size() &lt;&lt; "long";
  }
  QList&lt;QVariant&gt; results;
};

class C
{
public:
  C(QList&lt;int&gt; resultsList)
    : results{ resultsList } // &lt;&lt;&lt;&lt;&lt;&lt;
  {
     qDebug() &lt;&lt; "C QList&lt;int&gt; results are" &lt;&lt; results.size() &lt;&lt; "long";
  }
  QList&lt;int&gt; results;
};


int main(int , char *[])
{
    QList&lt;QVariant&gt; x;
    x &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3;

    A b1{ x };
    A b2(x);

    B c1{ x };
    B c2{ x };

    QList&lt;int&gt; i;
    i &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3;
    C a1{ i };
    C a2(i);
}
</code></pre>
<p dir="auto">with the output<br />
A results are 1 long<br />
A results are 1 long<br />
B results are 3 long<br />
B results are 3 long<br />
C QList&lt;int&gt; results are 3 long<br />
C QList&lt;int&gt; results are 3 long</p>
<p dir="auto">The behaviour is tied to QVariant, not QList&lt;&gt; as the "C" class results which use a QList&lt;int&gt; show.</p>
]]></description><link>https://forum.qt.io/topic/158625/unexpected-qlist-qvariant-initialiser-behaviour</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 00:40:19 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/158625.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 09 Sep 2024 18:18:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Tue, 10 Sep 2024 19:30:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> This is the kind of answer we pay for... :)</p>
]]></description><link>https://forum.qt.io/post/809220</link><guid isPermaLink="true">https://forum.qt.io/post/809220</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 10 Sep 2024 19:30:13 GMT</pubDate></item><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Tue, 10 Sep 2024 19:04:37 GMT]]></title><description><![CDATA[<p dir="auto">See <a href="https://stackoverflow.com/questions/68328782/constructor-taking-stdinitializer-list-is-preferred-over-other-constructors" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/68328782/constructor-taking-stdinitializer-list-is-preferred-over-other-constructors</a></p>
<p dir="auto">Basically the <code>std::initializer_list</code> constructor takes precedence over the copy constructor in class A (as it should by the standard)</p>
]]></description><link>https://forum.qt.io/post/809219</link><guid isPermaLink="true">https://forum.qt.io/post/809219</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Tue, 10 Sep 2024 19:04:37 GMT</pubDate></item><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Tue, 10 Sep 2024 17:17:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a> said in <a href="/post/809188">Unexpected QList&lt;QVariant&gt; Initialiser Behaviour</a>:</p>
<blockquote>
<p dir="auto">I think this is where you are not sure whether this is the expected behavior, right?</p>
</blockquote>
<p dir="auto">I don't know what behaviour to expect, frankly. As the behaviour is different for the stack variable case with different compilers it looks like the compiler writers weren't sure either, unless there are compiler-dependent conditionals in the QVariant code.</p>
<p dir="auto">I have tested this on 6.7.2 with VC2019 and MinGW, with the same results: {} initialising a stack allocated QList&lt;QVariant&gt; behaves differently to {} initialising a QList&lt;QVariant&gt; member variable of a stack allocated class instance.</p>
<p dir="auto">My only claim here is that this is tricky :-)</p>
]]></description><link>https://forum.qt.io/post/809213</link><guid isPermaLink="true">https://forum.qt.io/post/809213</guid><dc:creator><![CDATA[KenAppleby 0]]></dc:creator><pubDate>Tue, 10 Sep 2024 17:17:50 GMT</pubDate></item><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Tue, 10 Sep 2024 13:53:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chrisw67">@<bdi>ChrisW67</bdi></a></p>
<p dir="auto">I got intrigued and made some tests.</p>
<p dir="auto">This is my output on Windows where I print the function calls from <code>qlist.h</code>.<br />
(only have an older Qt / system at hand right now)</p>
<p dir="auto">Windows 10, 5.15.2, MSVC2019</p>
<pre><code>QList&lt;class QVariant&gt;::QList called
QList&lt;class QVariant&gt;::QList(InputIterator) called
QList&lt;class QVariant&gt;::QList(initializer_list) called
QList&lt;class QVariant&gt;::QList called
QList&lt;class QVariant&gt;::QList(InputIterator) called
QList&lt;class QVariant&gt;::QList(initializer_list) called
A results are length 1
\ -  QVariant(QVariantList, (QVariant(int, 1), QVariant(int, 2), QVariant(QString, "3")))
\ -  QVariant(int, 1)
\ -  QVariant(int, 2)
\ -  QVariant(QString, "3")
(QVariant(int, 1), QVariant(int, 2), QVariant(QString, "3"))
B results are length 3
\ -  QVariant(int, 1)
\ -  QVariant(int, 2)
\ -  QVariant(QString, "3")
Stack C results are length 3
\ -  QVariant(int, 1)
\ -  QVariant(int, 2)
\ -  QVariant(QString, "3")
</code></pre>
<p dir="auto">I assume the problem, if any, has to do with either the <code>QDebug</code> output (don't trust how it interprets data) or some <code>QVariant</code> voodoo, as it does not happen for STL containers.</p>
<p dir="auto">Made an <a href="https://godbolt.org/z/TTjrssn5c" target="_blank" rel="noopener noreferrer nofollow ugc">online test</a>. No issues</p>
<p dir="auto">Edit:</p>
<p dir="auto">Maybe this sequence of output</p>
<pre><code>QList&lt;class QVariant&gt;::QList called
QList&lt;class QVariant&gt;::QList(InputIterator) called
QList&lt;class QVariant&gt;::QList(initializer_list) called
</code></pre>
<p dir="auto">indicates that it encapsulates the <code>QVariantList</code> somehow and creates an "inner" list inside another <code>QVariantList</code> using the initializer_list c'tor...</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kenappleby-0">@<bdi>KenAppleby-0</bdi></a>  I think this is where you are not sure whether this is the expected behavior, right?</p>
]]></description><link>https://forum.qt.io/post/809188</link><guid isPermaLink="true">https://forum.qt.io/post/809188</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Tue, 10 Sep 2024 13:53:10 GMT</pubDate></item><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Tue, 10 Sep 2024 06:15:52 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">Why doesn't the same happen with the stack variable?</p>
</blockquote>
<p dir="auto">It does here (Linux, GCC 13, Qt 5.15.3 or 6.7.2):</p>
<pre><code>chrisw@newton:/tmp/tt$ ./tt 
A results are length 1
\ -  QVariant(QVariantList, (QVariant(int, 1), QVariant(int, 2), QVariant(QString, "3")))
B results are length 3
\ -  QVariant(int, 1)
\ -  QVariant(int, 2)
\ -  QVariant(QString, "3")
Stack C results are length 1
\ -  QVariant(QVariantList, (QVariant(int, 1), QVariant(int, 2), QVariant(QString, "3")))
</code></pre>
]]></description><link>https://forum.qt.io/post/809139</link><guid isPermaLink="true">https://forum.qt.io/post/809139</guid><dc:creator><![CDATA[ChrisW67]]></dc:creator><pubDate>Tue, 10 Sep 2024 06:15:52 GMT</pubDate></item><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Tue, 10 Sep 2024 05:20:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a> Why doesn't the same happen with the stack variable?</p>
<pre><code>QList&lt;QVariant&gt; C{ x };
</code></pre>
]]></description><link>https://forum.qt.io/post/809136</link><guid isPermaLink="true">https://forum.qt.io/post/809136</guid><dc:creator><![CDATA[KenAppleby 0]]></dc:creator><pubDate>Tue, 10 Sep 2024 05:20:56 GMT</pubDate></item><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Tue, 10 Sep 2024 04:45:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kenappleby-0">@<bdi>KenAppleby-0</bdi></a> said in <a href="/post/809131">Unexpected QList&lt;QVariant&gt; Initialiser Behaviour</a>:</p>
<blockquote>
<p dir="auto">A results are length 1</p>
<ul>
<li>QVariant(QVariantList, QList(QVariant(int, 1), QVariant(int, 2), QVariant(QString, "3")))</li>
</ul>
</blockquote>
<p dir="auto">Simple:<br />
You use X to initialize A, which uses the whole list to initalize the <code>results</code> member which becomes a single element list of <code>QList&lt;QVariant&gt;</code> == <code>QVariantList</code>.</p>
<p dir="auto">That's how I would explain this behavior.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a> made a good explanation <a href="https://forum.qt.io/post/798269">here</a></p>
]]></description><link>https://forum.qt.io/post/809132</link><guid isPermaLink="true">https://forum.qt.io/post/809132</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Tue, 10 Sep 2024 04:45:26 GMT</pubDate></item><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Tue, 10 Sep 2024 04:34:05 GMT]]></title><description><![CDATA[<p dir="auto">Sorry, I described this incorrectly.</p>
<p dir="auto">As you say <a class="plugin-mentions-user plugin-mentions-a" href="/user/axel-spoerl">@<bdi>Axel-Spoerl</bdi></a>, case A produces a QList&lt;QVariant&gt;, as it should do, of course, with one element which is a QVariant&lt;QList&lt;QVariant&gt;&gt;.</p>
<p dir="auto">This is fair enough, but tricky, I claim.</p>
<p dir="auto">Constructing a QList&lt;QVariant&gt; on the stack using the same {} method produces a different result:</p>
<pre><code>class A
{
public:
    A(QList&lt;QVariant&gt; resultsList)
      : results{ resultsList } // &lt;&lt;&lt;&lt;&lt;&lt;
    {
        qDebug() &lt;&lt; "A results are length" &lt;&lt; results.size();
        for (auto&amp; r : results) qDebug() &lt;&lt; "\\ - " &lt;&lt; r;
    }
    QList&lt;QVariant&gt; results;
};

class B
{
public:
    B(QList&lt;QVariant&gt; resultsList)
      : results(resultsList) // &lt;&lt;&lt;&lt;&lt;&lt;
    {
        qDebug() &lt;&lt; "B results are length" &lt;&lt; results.size();
        for (auto&amp; r : results) qDebug() &lt;&lt; "\\ - " &lt;&lt; r;
    }
    QList&lt;QVariant&gt; results;
};

int main(int , char *[])
{
    const QList&lt;QVariant&gt; x{ 1, 2, "3" };

    A a1{ x };
    B b1{ x };

    QList&lt;QVariant&gt; C{ x };

    qDebug() &lt;&lt; "Stack C results are length" &lt;&lt; C.size();
    for (auto&amp; r : C) qDebug() &lt;&lt; "\\ - " &lt;&lt; r;
}
</code></pre>
<p dir="auto">A results are length 1<br />
-  QVariant(QVariantList, QList(QVariant(int, 1), QVariant(int, 2), QVariant(QString, "3")))<br />
B results are length 3<br />
-  QVariant(int, 1)<br />
-  QVariant(int, 2)<br />
-  QVariant(QString, "3")<br />
Stack C results are length 3<br />
-  QVariant(int, 1)<br />
-  QVariant(int, 2)<br />
-  QVariant(QString, "3")</p>
]]></description><link>https://forum.qt.io/post/809131</link><guid isPermaLink="true">https://forum.qt.io/post/809131</guid><dc:creator><![CDATA[KenAppleby 0]]></dc:creator><pubDate>Tue, 10 Sep 2024 04:34:05 GMT</pubDate></item><item><title><![CDATA[Reply to Unexpected QList&lt;QVariant&gt; Initialiser Behaviour on Mon, 09 Sep 2024 21:08:21 GMT]]></title><description><![CDATA[<p dir="auto">Can you debug the list object itself, not just the size?<br />
In the case of A it is a nested variant list - one element which is a variant list itself.</p>
]]></description><link>https://forum.qt.io/post/809120</link><guid isPermaLink="true">https://forum.qt.io/post/809120</guid><dc:creator><![CDATA[Axel Spoerl]]></dc:creator><pubDate>Mon, 09 Sep 2024 21:08:21 GMT</pubDate></item></channel></rss>