Is there any way to expose a different 'parent' to children for anchor'ing to?



  • If I have a file, call it Background.qml, that looks a bit like this:

    @
    import QtQuick 1.0

    Rectangle {
    id: outer
    color: "red"

    Rectangle {
        id: inner
        color: "blue"
        anchors.margin: 100
        anchors.fill: parent
    }
    

    }
    @

    So what I've done there is define a widgets that is a blue rectangle with a 100 pixel red border. Now, if I use it somewhere else...

    @
    import QtQuick 1.0

    Background {
    width: 800
    Text {
    id: lorem_ipsum
    anchors.fill: parent
    text: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    }
    }
    @

    As I've written this here, it won't accomplish what I want to do. What I'd like is to define a re-usable background component that I can place in a qml file and then have the child contents (in this case the Text id:lorem_ipsum) appear anchored within the blue rectangle (Rectangle id:inner in Background.qml). As written, the text will end up being anchored within the red rectangle (Rectangle id:outer in Background.qml). I could do this:

    @import QtQuick 1.0

    Rectangle {
    id: outer
    color: "red"
    property alias client: inner

    Rectangle {
        id: inner
        color: "blue"
        anchors.margin: 100
        anchors.fill: parent
    }
    

    }
    @

    @
    import QtQuick 1.0

    Background {
    width: 800
    Text {
    id: lorem_ipsum
    anchors.fill: parent.client
    text: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    }
    }
    @

    But this just feels hacky. Surely there's some 'right' way to do this, no?



  • Never mind :), my suggestion was the same as yours.


Log in to reply
 

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