3

I came across a weird problem when designing my application in QML. The following code works:

TableView {
  itemDelegate: Item {
    function a() {}
    Loader {}
  }
}

I have a bunch of functions, properties, and loaders in this item delegate which is an Item object. Problems arise when I try to reuse this delegate in a ListView. I can't reference it like this:

Item {
    id: myitem
    function a() {}
    Loader {}
}

TableView {
   itemDelegate: myitem
}

error: Unable to assign QQuickItem to QQmlComponent

This is because itemDelegate is a Component:

http://qt-project.org/doc/qt-5/qml-qtquick-controls-tableview.html#itemDelegate-prop

So QML can convert an Item to Component when it's embedded, but not when it's referenced.

And I can't make it a component, since components can't have functions, loaders, properties etc.

How can I reuse my delegate?

Alex
  • 34,581
  • 26
  • 91
  • 135

1 Answers1

6

Solved by wrapping the item inside the component:

Component {
   Item{...}
}
Alex
  • 34,581
  • 26
  • 91
  • 135
  • I'm also struggling with this issue but your code is not working for me because I actually have a `list` of `Component`s. In my case I have a `PageIndicator` with a `list` of `Image` type components inside and I'm trying to assign an element of that `list` to the `delegate` property. Have you ever used a container for assigning a value to a `delegate`? – rbaleksandar Jul 22 '16 at 08:13