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. Delegate in another file
Qt 6.11 is out! See what's new in the release blog

Delegate in another file

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 3.6k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello
    I want to make a ListView where I refer the delegate from another file, but it doesn't seem to work.
    Is this not possible?
    For example

    main.qml
    @ListView {
    model: myModel
    delegate: MyDelegate
    }
    @
    MyDelegate.qml
    @Component{
    //Populate the list with myModel
    }@

    Thanks in advance ^^

    [Edit: Added @ tags around code -- mlong]

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Yes it is possible, although I'm not sure whether you should be using Component in the MyDelegate.qml. I don't have a handy example right here and now, but in general it usually works.

      (Z(:^

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        My example above, returns a reference error. It says that MyDelegate is not defined.
        So any ideas about how I can make this work?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          perhaps an include is missing?

          I'm back at my machine now, will try to create an example for you.

          (Z(:^

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            OK as I've said - it works. Both with delegate defining a Component and a plain Rectangle.

            Code (both files are in the same directory:
            @
            // main.qml
            import QtQuick 1.1

            Rectangle {
            width: 360
            height: 360

            ListView {
                anchors.fill: parent
            
                model: ListModel {
                    ListElement {
                        entryText: "Bla bla"
                    }
            
                    ListElement {
                        entryText: "Bla bla 2"
                    }
                }
            
                delegate: MyDeletage {}
            }
            

            }
            @

            And the delegate:
            @
            // MyDelegate.qml
            import QtQuick 1.1

            Component { // This is optional. It works with Rectangle, too
            Rectangle {
            width: 200
            height: 100
            border.color: "#000000"
            border.width: 2

                Text {
                    anchors.fill: parent
                    text: entryText
                }
            }
            

            }
            @

            (Z(:^

            1 Reply Last reply
            1

            • Login

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