<?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[Cant make Instantiator work with QList&lt;QObject*&gt;]]></title><description><![CDATA[<p dir="auto">I am developing for Android and trying to create a Menu with items populated dynamically from a model which is assigned from C++.</p>
<p dir="auto">QML I use is like this:<br />
@<br />
ApplicationWindow {<br />
menuBar: MenuBar {<br />
Menu {<br />
id: menu<br />
Instantiator {<br />
id: menuInstantiator<br />
MenuItem {<br />
text: model.title || ""<br />
}<br />
onObjectAdded: menu.insertItem(index, object)<br />
onObjectRemoved: menu.removeItem(object)<br />
}<br />
}<br />
}<br />
}<br />
@</p>
<p dir="auto">C++ code is like this:<br />
@<br />
class Info: public QObject<br />
{<br />
Q_OBJECT</p>
<pre><code>// Properties for use in QML
Q_PROPERTY(QString id READ id)
Q_PROPERTY(QString title READ title)
</code></pre>
<p dir="auto">public:<br />
Info(const QString&amp; id, QObject *parent = 0);</p>
<pre><code>QString id() const { return m_id; }
QString title() const { return m_title; }
</code></pre>
<p dir="auto">private:<br />
QString m_id;<br />
QString m_title;<br />
};</p>
<p dir="auto">...<br />
QList&lt;QObject*&gt; infos;<br />
infos.append(new Info(1));<br />
infos.append(new Info(2));<br />
QObject* menu-&gt;setProperty("model", QVariant::fromValue(infos));<br />
...</p>
<p dir="auto">@</p>
<p dir="auto">The problem is that this is not working - clicking on the menu bar button does not open the menu. As a side effect other menus also stop working.</p>
<p dir="auto">Similar code with QStringList is working fine. However i want to show one string as item text and pass a different string as item id for triggered event. Thats why I use Info object instead of single string per item</p>
<p dir="auto">What am I doing wrong?</p>
]]></description><link>https://forum.qt.io/topic/41694/cant-make-instantiator-work-with-qlist-qobject</link><generator>RSS for Node</generator><lastBuildDate>Sat, 16 May 2026 10:38:41 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/41694.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 21 May 2014 20:08:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Sun, 08 Jun 2014 14:59:56 GMT]]></title><description><![CDATA[<p dir="auto">Thanks, I'll check this</p>
]]></description><link>https://forum.qt.io/post/231316</link><guid isPermaLink="true">https://forum.qt.io/post/231316</guid><dc:creator><![CDATA[shukurmukur]]></dc:creator><pubDate>Sun, 08 Jun 2014 14:59:56 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Fri, 06 Jun 2014 18:42:38 GMT]]></title><description><![CDATA[<p dir="auto">I discovered that template into the documentation:<br />
QQmlListProperty</p>
<p dir="auto">Maybe can be a solution for your problem.</p>
]]></description><link>https://forum.qt.io/post/231209</link><guid isPermaLink="true">https://forum.qt.io/post/231209</guid><dc:creator><![CDATA[Gianluca]]></dc:creator><pubDate>Fri, 06 Jun 2014 18:42:38 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Tue, 27 May 2014 20:55:10 GMT]]></title><description><![CDATA[<p dir="auto">Humm... maybe we are both stupid... because I tried lot in the past and the QList&lt;QObject*&gt; never worked. That's why I developed the workaround I posted.<br />
I'll do some further test.</p>
]]></description><link>https://forum.qt.io/post/230092</link><guid isPermaLink="true">https://forum.qt.io/post/230092</guid><dc:creator><![CDATA[Gianluca]]></dc:creator><pubDate>Tue, 27 May 2014 20:55:10 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Tue, 27 May 2014 20:48:52 GMT]]></title><description><![CDATA[<p dir="auto">Well, I am not sure if QListObject is really needed as documentation suggests that QList&lt;QObject*&gt; should work: <a href="http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html</a>. That makes me think that my case is either a bug or I am doing something stupid with the later being more likely as I am really new to QML and Qt.</p>
]]></description><link>https://forum.qt.io/post/230091</link><guid isPermaLink="true">https://forum.qt.io/post/230091</guid><dc:creator><![CDATA[shukurmukur]]></dc:creator><pubDate>Tue, 27 May 2014 20:48:52 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Mon, 26 May 2014 19:53:38 GMT]]></title><description><![CDATA[<p dir="auto">I think that for get QList&lt;QObject*&gt; work we should file a feature request and to add a derived type QListObjects similar to QStringList and making it usable into QML as QStringList.</p>
]]></description><link>https://forum.qt.io/post/229925</link><guid isPermaLink="true">https://forum.qt.io/post/229925</guid><dc:creator><![CDATA[Gianluca]]></dc:creator><pubDate>Mon, 26 May 2014 19:53:38 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Mon, 26 May 2014 19:50:08 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for the tips, Gianluca, it helped. Although it would be nicer if QList&lt;QObject*&gt; version worked</p>
]]></description><link>https://forum.qt.io/post/229924</link><guid isPermaLink="true">https://forum.qt.io/post/229924</guid><dc:creator><![CDATA[shukurmukur]]></dc:creator><pubDate>Mon, 26 May 2014 19:50:08 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Mon, 26 May 2014 06:49:55 GMT]]></title><description><![CDATA[<p dir="auto">Ah, I was forgetting... PAY ATTENTION TO THE OWNERSHIP OF RETURNING QOBJECT* :</p>
<p dir="auto"><a href="http://qt-project.org/doc/qt-5/qtqml-cppintegration-data.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5/qtqml-cppintegration-data.html</a></p>
<p dir="auto">It's an important aspect that if taken not seriously into account will crash your app.</p>
]]></description><link>https://forum.qt.io/post/229844</link><guid isPermaLink="true">https://forum.qt.io/post/229844</guid><dc:creator><![CDATA[Gianluca]]></dc:creator><pubDate>Mon, 26 May 2014 06:49:55 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Mon, 26 May 2014 06:47:05 GMT]]></title><description><![CDATA[<p dir="auto">I always get problem on passing QList&lt;QObject*&gt; to QML side too.<br />
I think that the problem is due to the fact that QML for some reason cannot properly see a custom QList instantiation.<br />
But, as you notice too, with QStringList everything works fine.<br />
So, my workaround to passing a complex object is to use a QStringList as a list of object IDs and use a slot for getting the corresponding QObject.<br />
Something like that:<br />
@<br />
Instantiator {<br />
id: menuInstantiator<br />
MenuItem {<br />
property InfoObject obj: backend.getInfoObject( modelData )<br />
text: obj.title || ""<br />
}<br />
}<br />
@<br />
where backend is a QObject globally visible to QML with a slot that return a QObject* given an QString id. And the modelData is one of the element of the QStringList that you pass from C++ to QML.</p>
<p dir="auto">Important: InfoObject has to declare the properties via Q_PROPERTY and to registered with qmlRegisterType otherwise QML cannot see it properly.</p>
]]></description><link>https://forum.qt.io/post/229843</link><guid isPermaLink="true">https://forum.qt.io/post/229843</guid><dc:creator><![CDATA[Gianluca]]></dc:creator><pubDate>Mon, 26 May 2014 06:47:05 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Sat, 24 May 2014 18:01:46 GMT]]></title><description><![CDATA[<p dir="auto">Yes, my bad. I changed the code a lot and that code was not the initial code I was expecting to work.</p>
<p dir="auto">Originally on ApplicationWindow i declared alias property:<br />
@<br />
property alias menuModel: menuInstantiator.model<br />
@</p>
<p dir="auto">and  in c++ I called:<br />
@<br />
QObject* root-&gt;setProperty("menuModel", QVariant::fromValue(infos));<br />
@</p>
<p dir="auto">However the important point I think is that the same code worked with QStringList, but not with QList&lt;Object*&gt;. Of course expression for MenuItem.text property should be different for QStringList and QList&lt;Object*&gt;, but I would expect only empty or incorrect texts in later case. Now menu is not showing at all</p>
]]></description><link>https://forum.qt.io/post/229720</link><guid isPermaLink="true">https://forum.qt.io/post/229720</guid><dc:creator><![CDATA[shukurmukur]]></dc:creator><pubDate>Sat, 24 May 2014 18:01:46 GMT</pubDate></item><item><title><![CDATA[Reply to Cant make Instantiator work with QList&lt;QObject*&gt; on Fri, 23 May 2014 08:52:44 GMT]]></title><description><![CDATA[<p dir="auto">While I didn't test your code, it looks like you are setting the "model" property on the Menu when you should really be setting it on the "menuInstantiator" itself.</p>
<p dir="auto">See the bottom of <a href="http://qt-project.org/doc/qt-5/qml-qtquick-controls-menu.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5/qml-qtquick-controls-menu.html</a> for an example.</p>
]]></description><link>https://forum.qt.io/post/229572</link><guid isPermaLink="true">https://forum.qt.io/post/229572</guid><dc:creator><![CDATA[Jens]]></dc:creator><pubDate>Fri, 23 May 2014 08:52:44 GMT</pubDate></item></channel></rss>