Why wrap an inline delegate in a Component?



  • In some places in the Qt documentation I see constructions like this one (taken from the "QML Scope":http://doc-snapshot.qt-project.org/4.8/qdeclarativescope.html page):

    @
    PathView {
    delegate: Component {
    Rectangle {
    id: root
    Image {
    scale: root.PathView.scale
    }
    }
    }
    }
    @

    My question is, what is the use of the Component element encapsulating the Rectangle element here? Why not use the Rectangle element as delegate directly, i.e.:

    @
    PathView {
    delegate: Rectangle {
    id: root
    Image {
    scale: root.PathView.scale
    }
    }
    }
    @

    Is there any difference between those two approaches, and if so, when should I chose the first and when the second?



  • Hello,

    To my humble point of view, since the property delegate itself is a component, there is no difference between the two, (i.e. Component could be almost replaced by Item in the example above).



  • As per the previous comment, there is no difference. If the required type is 'Component' but the provided type is not a Component, a Component is created automatically for your convenience. I'd stick with the second just because its nicer to read and less to type.



  • Thanks! I assumed it was something like that, but couldn't quite understand the details. Now I do.


Log in to reply
 

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