Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Reusing a delegate component in QML
Forum Update on Monday, May 27th 2025

Reusing a delegate component in QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    a5med12
    wrote on 31 Jul 2014, 15:15 last edited by
    #1

    0 down vote favorite

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on 31 Jul 2014, 15:49 last edited by
      #2

      You just need to wrap your item in a Component:

      @Component {Item{...}}@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        a5med12
        wrote on 31 Jul 2014, 16:00 last edited by
        #3

        Thanks so much! It worked.

        I'm new to Qt, so you saved me a lot of time :)

        Thanks again.

        1 Reply Last reply
        0

        1/3

        31 Jul 2014, 15:15

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved