Note that there are some explanatory texts on larger screens.

plurals
  1. POQML: How can I pass model properties to a delegate loaded inside a GridView (or ListView) delegate?
    primarykey
    data
    text
    <p>I'm attempting to build a reusable QML Component that internally hosts a GridView. Each element in the GridView has a set of behaviors (mostly display and mouse-based stuff) that is common throughout the application. However, what is displayed inside the GridView elements changes depending on the use-case. (That is, the same encapsulated Component for one GridView, but elsewhere in the application it might use a different Component.)</p> <p>So, what I would like to do is have each invocation supply a delegate that is added to each element in the GridView, which is already a delegate. In other words, something like this:</p> <p><strong>MyGrid.qml</strong></p> <pre><code>import QtQuick 1.1 Rectangle { id: myGrid objectName: "myGrid" property Component internalDelegate property variant internalModel GridView { anchors.fill: parent delegate: Row { Loader { sourceComponent: myGrid.internalDelegate } } model: parent.internalModel } } </code></pre> <p>The idea is that the Loader inside the GridView delegate loads the user-supplied delegate, which would look something like this:</p> <p><strong>Main.qml</strong></p> <pre><code>import QtQuick 1.1 Rectangle { anchors.fill: parent width: 300 height: 200 MyGrid { anchors.fill: parent internalDelegate: Text { text: name } internalModel: ListModel { ListElement { name: "a" } ListElement { name: "b" } } } } </code></pre> <p>However, this doesn't work. QML reports that "name" is an unknown variable inside the Text element. If I replace the name variable with a string (i.e. "hello"), it works as expected.</p> <p>My question is, how can I pass the "name" variable to internalDelegate, or better yet, make the entire Model available so internalDelegate can access all of them (since the caller is defining the model as well).</p> <p>A side question is: Is there a better way to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload