<?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[Qobject_cast triggers an assertion on QML components [SOLVED]]]></title><description><![CDATA[<p dir="auto">I'm using this code to iterate through the children of my custom object (C++, derived from QObject) and check for a certain type:</p>
<p dir="auto">@<br />
foreach (QObject *o, children()) {<br />
Foo <em>f = qobject_cast&lt;Foo</em>&gt;(o);<br />
if (f) {<br />
// do foo stuff with f<br />
}<br />
}<br />
@</p>
<p dir="auto">This works great as long as there are only C++ objects in children(). However, when I make a component that extends a "Foo", like this:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/import">@<bdi>import</bdi></a> FooLib 1.0</p>
<p dir="auto">Foo {<br />
...<br />
}<br />
@</p>
<p dir="auto">qobject_cast&lt;Foo *&gt; triggers an assertion failure:</p>
<p dir="auto">@ASSERT: "!dp-&gt;isFunction()" in file qml/qqmlpropertycache.cpp, line 1509@</p>
<p dir="auto">Here's a stack trace:</p>
<p dir="auto">@148 raise()  0x0000003562a32885<br />
147 abort()  0x0000003562a34065<br />
146 qt_message_fatal() qlogging.cpp:806 0x00007ffff6758da9<br />
145 QMessageLogger::fatal() qlogging.cpp:355 0x00007ffff6756ffa<br />
144 qt_assert() qglobal.cpp:1951 0x00007ffff675233c<br />
143 QQmlPropertyCache::toMetaObjectBuilder() qqmlpropertycache.cpp:1509 0x00007ffff7c21db5<br />
142 QQmlPropertyCache::createMetaObject() qqmlpropertycache.cpp:528 0x00007ffff7c1dc69<br />
141 QQmlVMEMetaObject::toDynamicMetaObject() qqmlvmemetaobject.cpp:539 0x00007ffff7b66944<br />
140 QObjectData::dynamicMetaObject() qobject.cpp:189 0x00007ffff69732f4<br />
139 ShotgunEntity::metaObject() moc_shotgunentity.cpp:100 0x00007fffec20e16b<br />
138 QMetaObject::cast() qmetaobject.cpp:338 0x00007ffff6941eda<br />
137 qobject_cast&lt;Property*&gt;() qobject.h:451 0x00007fffec1d03b6<br />
136 NodeBase::inputs() nodebase.cpp:364 0x00007fffec1cf758 @</p>
<p dir="auto">Am I doing something wrong?</p>
<p dir="auto">thanks,<br />
-Mark</p>
]]></description><link>https://forum.qt.io/topic/21176/qobject_cast-triggers-an-assertion-on-qml-components-solved</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 01:30:46 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/21176.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 06 Nov 2012 22:54:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qobject_cast triggers an assertion on QML components [SOLVED] on Wed, 07 Nov 2012 19:32:35 GMT]]></title><description><![CDATA[<p dir="auto">Solved!</p>
<p dir="auto">I figured it out after being unable to reproduce it in a simplified test case.</p>
<p dir="auto">The base class of Foo has a default property called "data", patterned after QQmlItem. I had forgotten this and added an invokable method, also called "data", to Foo. In concrete instances of Foo, this wasn't a problem because they had different method signatures, but as soon as I started using QML instances, the invokable clobbered the default property.</p>
]]></description><link>https://forum.qt.io/post/156328</link><guid isPermaLink="true">https://forum.qt.io/post/156328</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 07 Nov 2012 19:32:35 GMT</pubDate></item><item><title><![CDATA[Reply to Qobject_cast triggers an assertion on QML components [SOLVED] on Wed, 07 Nov 2012 14:39:10 GMT]]></title><description><![CDATA[<p dir="auto">bq. My guess is that in QML, inheritance works differently, for example through composition.</p>
<p dir="auto">Good point, I think they're all internally instances of QQmlComponent or some such. I'll have to figure out a different way to check if a QObject conforms to a particular interface.</p>
<p dir="auto">bq. The QMetaObject is then created on demand (In this case the qobject_cast).</p>
<p dir="auto">Well, it tries to create the metaobject on demand, anyway. I'm not sure why, but QML component objects do not have an associated QMetaObject. I discovered this when trying to see how hard it would be to add support for native QML attached properties objects (answer: hard enough that I gave up).</p>
<p dir="auto">I'll whip up a small test case when I have a chance and file a bug report.</p>
<p dir="auto">So bug aside, anyone know how to check if a QML instance conforms to an interface?</p>
<p dir="auto">edit: I tried guarding the qobject_cast with<br />
@if (o-&gt;inherits("Foo"))@</p>
<p dir="auto">The qobject_cast still triggers an assertion for QML instances "derived" from Foo.</p>
]]></description><link>https://forum.qt.io/post/156302</link><guid isPermaLink="true">https://forum.qt.io/post/156302</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 07 Nov 2012 14:39:10 GMT</pubDate></item><item><title><![CDATA[Reply to Qobject_cast triggers an assertion on QML components [SOLVED] on Wed, 07 Nov 2012 14:18:27 GMT]]></title><description><![CDATA[<p dir="auto">This seems to be a bug in QML. The meta data from QML Components is stored in a structure called QQmlPropertyCache. The QMetaObject is then created on demand (In this case the qobject_cast).</p>
<p dir="auto">The creation of the QMetaObject seems to fail/assert in your case.</p>
]]></description><link>https://forum.qt.io/post/156299</link><guid isPermaLink="true">https://forum.qt.io/post/156299</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 07 Nov 2012 14:18:27 GMT</pubDate></item><item><title><![CDATA[Reply to Qobject_cast triggers an assertion on QML components [SOLVED] on Wed, 07 Nov 2012 05:42:47 GMT]]></title><description><![CDATA[<p dir="auto">This is because your QML type is not "Foo". It's some internal type like QMLTYPE_123. You can check</p>
<p dir="auto">@<br />
Foo {<br />
id: object<br />
Component.onCompleted: console.log(object)<br />
}<br />
@</p>
<p dir="auto">My guess is that in QML, inheritance works differently, for example through composition.</p>
]]></description><link>https://forum.qt.io/post/156243</link><guid isPermaLink="true">https://forum.qt.io/post/156243</guid><dc:creator><![CDATA[beemaster]]></dc:creator><pubDate>Wed, 07 Nov 2012 05:42:47 GMT</pubDate></item></channel></rss>