<?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[Use a custom item as a children declared as a property (like delegate in views)]]></title><description><![CDATA[<p dir="auto">I'm trying to create an object where you can declare a custom item that will be inserted in my custom object. But I don't know how to use this delegate later in my code.<br />
Example:</p>
<pre><code>// MyObject.qml
Row {
    property var customItem
    ItemBefore { }
    // This where I don't know how to use the custom item
    customItem {
        // That would be even better if I could set properties 
        // that are used by the item (width, height, ...)
        // like with a regular QML object
        someProperties: true
    }
    ItemAfter { }
}

// Main.qml
Item {
    MyObject {
        customItem: Rectangle { color: "#FF0000" }
    }
    MyObject {
        customItem: Rectangle { color: "#00FF00" }
    }
    MyObject {
        customItem: Rectangle { color: "#0000FF" }
    }
}
</code></pre>
<p dir="auto">The behavior I'm looking for is similar to the one used by <code>ListView</code> and <code>GridView</code> with the <code>delegate</code> property.<br />
Can anyone help me ?<br />
MoaMoaK</p>
]]></description><link>https://forum.qt.io/topic/84921/use-a-custom-item-as-a-children-declared-as-a-property-like-delegate-in-views</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 05:23:17 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/84921.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 10 Nov 2017 10:42:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Use a custom item as a children declared as a property (like delegate in views) on Thu, 16 Nov 2017 16:44:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dubu">@<bdi>DuBu</bdi></a> Sorry I'm a bit late, but really using Loader was THE solution I wanted, thx a lot.<br />
It works perfectly.</p>
]]></description><link>https://forum.qt.io/post/426492</link><guid isPermaLink="true">https://forum.qt.io/post/426492</guid><dc:creator><![CDATA[MoaMoaK]]></dc:creator><pubDate>Thu, 16 Nov 2017 16:44:50 GMT</pubDate></item><item><title><![CDATA[Reply to Use a custom item as a children declared as a property (like delegate in views) on Sat, 11 Nov 2017 12:17:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/moamoak">@<bdi>MoaMoaK</bdi></a> Using a Loader should perfectly do want you want to achieve:</p>
<pre><code>// MyObject.qml
Row {
    property alias customItem: loader.sourceComponent
    ItemBefore { }
    Loader {
      id: loader
      // Just a bunch of custom properties
      property int widthOfCustomItem: 100
      property int heightOfCustomItem: 100
    }
    ItemAfter { }
}

// Main.qml
Item {
    MyObject {
        customItem: Rectangle {
          color: "#FF0000"
          // You can use all properties of the Loader inside your customItem
          width: widthOfCustomItem
          height: heightOfCustomItem
        }
    }
    MyObject {
        customItem: Rectangle { color: "#00FF00" }
    }
    MyObject {
        customItem: Rectangle { color: "#0000FF" }
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/post/425445</link><guid isPermaLink="true">https://forum.qt.io/post/425445</guid><dc:creator><![CDATA[DuBu]]></dc:creator><pubDate>Sat, 11 Nov 2017 12:17:14 GMT</pubDate></item><item><title><![CDATA[Reply to Use a custom item as a children declared as a property (like delegate in views) on Sat, 11 Nov 2017 00:00:52 GMT]]></title><description><![CDATA[<p dir="auto">The easiest way I can think to do this in pure QML is use a <a href="http://doc.qt.io/qt-5/qml-qtquick-repeater.html" target="_blank" rel="noopener noreferrer nofollow ugc"><code>Repeater</code></a> instead of your custom object. It takes a delegate but does not impose any particular layout. You can let the items position themselves (e.g. using x/y properties in the delegate based on the model) or you can control their layout yourself by using the <a href="http://doc.qt.io/qt-5/qml-qtquick-repeater.html#itemAdded-signal" target="_blank" rel="noopener noreferrer nofollow ugc"><code>itemAdded</code> signal</a> and adjusting properties of the <code>item</code> there.</p>
<p dir="auto">But assuming that does not meet your needs, and since it does not answer your question, I would wrap your delegate in a <a href="http://doc.qt.io/qt-5/qml-qtqml-component.html" target="_blank" rel="noopener noreferrer nofollow ugc"><code>Component</code></a> so that you can easily instantiate it using <a><code>createObject()</code></a>:</p>
<p dir="auto"><code>main.qml</code></p>
<pre><code>import QtQuick 2.6
import QtQuick.Window 2.2
Window {
    visible:true; width:640; height:480; title:"Replicator"
    Replicator {
        copies: 15
        Component {
            Rectangle {
                width:200; height:100
                color:Qt.rgba(255,0,0,0.2); border.color:'red'
                property alias label: t.text
                Text { id:t; anchors.centerIn:parent }
            }
        }
    }
}
</code></pre>
<p dir="auto"><code>Replicator.qml</code></p>
<pre><code>import QtQuick 2.0
Item {
    id: root
    anchors.fill: parent
    property int copies: 5
    default property var template
    Component.onCompleted: {
        for (var i=copies;i--;) {
            var props = {
                label: "I'm item #"+i,
                x: Math.random()*root.width/2,
                y: Math.random()*root.height/2
            };
            template.createObject(root, props);
        }
    }
}
</code></pre>
<p dir="auto">This produces a result like:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/24c91bd2-7f47-40a5-8cae-51644b6d5056.png" alt="0_1510358350481_Screen Shot 2017-11-10 at 4.58.29 PM.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">In the example above I've used the <a href="http://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html#default-properties" target="_blank" rel="noopener noreferrer nofollow ugc"><code>default</code> keyword</a> to allow your delegate without a label. This is purely optional.</p>
<p dir="auto">There may be a way to do this without a <code>Component</code>, but I don't know what it is. The page on <a href="http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html" target="_blank" rel="noopener noreferrer nofollow ugc">Dynamic QML Object Creation</a> says:</p>
<blockquote>
<p dir="auto"><em>There are two ways to create objects dynamically from JavaScript. You can either call <code>Qt.createComponent()</code> to dynamically create a Component object, or use <code>Qt.createQmlObject()</code> to create an object from a string of QML.</em></p>
</blockquote>
<p dir="auto">You don't have a string of QML, and so you must use a component.</p>
<p dir="auto">A true delegate object like ListView and Repeater accept would require some way to derive a component from an instance, and that does not appear to me to be possible in pure QML.</p>
]]></description><link>https://forum.qt.io/post/425415</link><guid isPermaLink="true">https://forum.qt.io/post/425415</guid><dc:creator><![CDATA[Phrogz]]></dc:creator><pubDate>Sat, 11 Nov 2017 00:00:52 GMT</pubDate></item></channel></rss>