<?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[Fastest way to compare QVariant with UserType]]></title><description><![CDATA[<p dir="auto">I'm looking for a very fast way to compare two QVariants that wrap the same user type.</p>
<p dir="auto">What do you think about the following approaches:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/bool">@<bdi>bool</bdi></a> operator==() const(const QVariant &amp;rhs)<br />
{<br />
if(myData.userType() == qMetaTypeId&lt;MyUserType&gt;())<br />
{<br />
bool method1 = <em>static_cast&lt;MyUserType</em>&gt;(myData.constData())  == <em>static_cast&lt;const MyUserType</em>&gt;(rhs.myData.constData());</p>
<p dir="auto">bool method2 = myData.value&lt;MyUserType&gt;() == rhs.myData.value&lt;MyUserType&gt;();<br />
}<br />
return something<br />
}@</p>
<p dir="auto">I would think that the first approach is faster, because no deep copy or ctor call is necessary. It is more risky because of the static_cast and kind of a hack though.</p>
<p dir="auto">I took a look into the Qt implementation of the second method, but as far as I could see, at least once the copy ctor of MyUserType is called.</p>
]]></description><link>https://forum.qt.io/topic/28261/fastest-way-to-compare-qvariant-with-usertype</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Apr 2026 05:09:50 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/28261.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 13 Jun 2013 14:23:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Fastest way to compare QVariant with UserType on Thu, 13 Jun 2013 15:25:58 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for the answers guys.</p>
<p dir="auto">I took the advice to compare the two approaches by stepping through them. The result is pretty clear.<br />
The first approach first calls QVariant::constData() twice (for each side) and then MyClass::operator==() is called.</p>
<p dir="auto">The second approach takes several steps, including multiple checks and a ctor call for each side before !!! method 1 is used (with a reinterpret_cast instead of a static_cast though).</p>
<p dir="auto">I think I'll stick to the first method, because it is faster and the comparing of the MetaTypeIds beforehand should in my case be enough to avoid illegal static_casts.</p>
]]></description><link>https://forum.qt.io/post/182705</link><guid isPermaLink="true">https://forum.qt.io/post/182705</guid><dc:creator><![CDATA[H.Klingel]]></dc:creator><pubDate>Thu, 13 Jun 2013 15:25:58 GMT</pubDate></item><item><title><![CDATA[Reply to Fastest way to compare QVariant with UserType on Thu, 13 Jun 2013 14:30:37 GMT]]></title><description><![CDATA[<p dir="auto">i would also tend to the second comparison.<br />
But you should add "QVariant::canConvert()":<a href="http://qt-project.org/doc/qt-4.8/qvariant.html#canConvert" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-4.8/qvariant.html#canConvert</a> checks before comparing the values.</p>
]]></description><link>https://forum.qt.io/post/182696</link><guid isPermaLink="true">https://forum.qt.io/post/182696</guid><dc:creator><![CDATA[raven-worx]]></dc:creator><pubDate>Thu, 13 Jun 2013 14:30:37 GMT</pubDate></item><item><title><![CDATA[Reply to Fastest way to compare QVariant with UserType on Thu, 13 Jun 2013 14:27:47 GMT]]></title><description><![CDATA[<p dir="auto">Well... why don't you try and see? Seems like something to just try out and doing some measurements on.</p>
]]></description><link>https://forum.qt.io/post/182693</link><guid isPermaLink="true">https://forum.qt.io/post/182693</guid><dc:creator><![CDATA[andre]]></dc:creator><pubDate>Thu, 13 Jun 2013 14:27:47 GMT</pubDate></item></channel></rss>