<?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[Unable to assign GenericDataModel to GenericDataModel]]></title><description><![CDATA[<p dir="auto">I'm developing a large Qt-based framework, so I'll try to give watered-down minimal examples where possible.<br />
I have a plugin system for defining new data models for viewing though custom QML view components.</p>
<p dir="auto">These plugins are dynamically loaded using <strong>QPluginLoader</strong>, as with the <a href="http://doc.qt.io/qt-4.8/qt-tools-plugandpaint-example.html" target="_blank" rel="noopener noreferrer nofollow ugc">Plug 'n' Paint</a> example. In my plugin interface, I can call a function that supplies me with a pointer to a newly allocated base class:</p>
<pre><code>GenericDataModel *mdl = mod-&gt;CreateModel("frt");
</code></pre>
<p dir="auto">where  <em>mod</em> is a pointer to the loaded plugin interface. <strong>GenericDataModel</strong> is a derived class of <strong>QAbstractItemModel</strong>, already confirmed to work with other models. <strong>GenericDataModel</strong> is also registered as a type with QML:</p>
<pre><code>qmlRegisterType&lt;GenericDataModel&gt;("plugin.genericmodel", 1,  0, "GenericDataModel");
</code></pre>
<p dir="auto">This works fine, and I then load the data into the model without any problems. I confirm with the debugger that the data is there and laid out correctly. I then register the model into QML like so:</p>
<pre><code>QQmlApplicationEngine engine;
QQmlContext *ctxt = engine.rootContext();
ctxt-&gt;setContextProperty("MDLFileTest", QVariant::fromValue(mdl));
</code></pre>
<p dir="auto">I then apply this to a custom QML view, <em>CustomList.qml</em>:</p>
<pre><code>import QtQuick 2.7
import QtQml.Models 2.2
import QtQuick.Controls 2.2
import plugin.genericmodel 1.0

ListView {
    id: listView
    property GenericDataModel dataModel

    model: dataModel
    delegate { ... }
}
</code></pre>
<p dir="auto">Which I then apply to a page:</p>
<pre><code>CustomList {
    id: customList
    dataModel: MDLFileTest
}
</code></pre>
<p dir="auto">When loading this from a plugin (i.e. the plugin defines a class that extends <strong>GenericDataModel</strong>, and allocates one and returns it as a <strong>GenericDataModel</strong>* to the main application), I get the QML error at runtime:</p>
<p dir="auto"><em>Unable to assign GenericDataModel to GenericDataModel</em></p>
<p dir="auto">At this point, as far as the QML layer is concerned, I've assigned a <strong>GenericDataModel</strong> for a property expecting a <strong>GenericDataModel</strong>. However, when I do exactly the same thing, but move the header and source file for the derived class into the main application, so that it is compiled in, it works perfectly...</p>
<p dir="auto">Can somebody explain this error to me? I've seen it before with two different types that cannot be casted, but this is an error trying to assign type <em>X</em> to type <em>X</em>! Quite strange. I have played around with the Q_OBJECT macro and gotten it to read:</p>
<p dir="auto"><em>Unable to assign FRTFileModel to GenericDataModel</em></p>
<p dir="auto">The strange thing is, <strong>FRTFileModel</strong> is a direct descendant of <strong>GenericDataModel</strong>!</p>
<p dir="auto">I tried to keep this question as minimal as possible, but if anyone wants me to try to create a minimal example project I can give it a go as well.<br />
Thanks!</p>
]]></description><link>https://forum.qt.io/topic/80151/unable-to-assign-genericdatamodel-to-genericdatamodel</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 12:33:43 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/80151.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 09 Jun 2017 21:40:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Unable to assign GenericDataModel to GenericDataModel on Mon, 12 Jun 2017 18:03:43 GMT]]></title><description><![CDATA[<p dir="auto">Interestingly, if I make the plugin static, it works fine. Only when the plugin is dynamic does this error happen... Very strange.</p>
]]></description><link>https://forum.qt.io/post/399042</link><guid isPermaLink="true">https://forum.qt.io/post/399042</guid><dc:creator><![CDATA[LScott]]></dc:creator><pubDate>Mon, 12 Jun 2017 18:03:43 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to assign GenericDataModel to GenericDataModel on Mon, 12 Jun 2017 17:49:37 GMT]]></title><description><![CDATA[<p dir="auto">I feel like I have somehow created a situation where there are 2 class types with the name "<strong>GenericDataModel</strong>"... The <strong>GenericDataModel</strong> class is declared and defined inside a seperate library that is statically linked to both the application and the plugin. Calling</p>
<pre><code>mdl-&gt;inherits("GenericDataModel");
</code></pre>
<p dir="auto">returns <em>true</em>, yet calling</p>
<pre><code>qobject_cast&lt;GenericDataModel*&gt;(mdl);
</code></pre>
<p dir="auto">returns <em>null</em>.</p>
<p dir="auto">Am I correct in thinking that this means that I have 2 separate class types that have the same name in the Qt meta object system? Obviously this is not what I am intending... How can I fix this? Both the plugin and the application include the same <em>genericdatamodel.h</em> from a static library, and both link to the same static library? Is this not the way that I should be doing this?</p>
<p dir="auto">Please give your input, thanks.</p>
]]></description><link>https://forum.qt.io/post/399041</link><guid isPermaLink="true">https://forum.qt.io/post/399041</guid><dc:creator><![CDATA[LScott]]></dc:creator><pubDate>Mon, 12 Jun 2017 17:49:37 GMT</pubDate></item></channel></rss>