Delegate in another file



  • 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]


  • Moderators

    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.



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


  • Moderators

    perhaps an include is missing?

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


  • Moderators

    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
        }
    }
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.