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. Is there any way to expose a different 'parent' to children for anchor'ing to?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.0k 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.
  • T Offline
    T Offline
    tedmiddleton
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0

      • Login

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