[Qt-qml] Reparent in object initialization

Artem Marchenko artem.marchenko at gmail.com
Tue Jun 14 21:07:00 CEST 2011


Hi

I didn't try this code, but in general it should work.

What I did try is to defined some parts of a scene in the descendant object (like you would do in a Strategy pattern for coding) and reparenting works very well there, even anchors are accounted correctly. For example, in my cross-platform app I need to draw status line on Symbian only: 

BaseUi.qml 
Rectangle {

  // to be filled for platforms that need it
  property alias statusLine: mainPane.statusLine

  // needs to be exposed so that I could parent to it
  property alias mainPane: mainPane

  Rectangle {
    id: mainPane
    property variant statusLine: null
  }
}

SymbianUi.qml

BaseUi {
  statusLine: Rectangle {
    parent: mainPane
     anchors.left: parent.left  // yep, this also works
  }
}

The only drawback is that when SymbianUi is instantiated, first it fully constructs the BaseUi and runs its onCompleted handler and only then SymbianUi is completed. It means that from BaseUi you can't refer to statusLine even if you are 100% sure it's going to be created.

I hope it helps.

Best regards,
Artem.

On Jun 14, 2011, at 4:54 PM, Adriano Rezende wrote:

> Hi,
> 
> Is that ok to set the parent of an object during the property initialization?
> 
> For example:
> 
> // Foo.qml
> Item {
>     property alias container: frame
> 
>     Item {
>         id: frame
>         anchors.fill: parent
>     }
> 
>     Rectangle {
>         anchors.fill: parent
>         color: "red"
>         opacity: 0.5
>     }
> }
> 
> // main.qml
> Foo {
>     id: foo
>     Rectangle {
>         parent: foo.container
>         color: "blue"
>     }
> }
> 
> 
> QML would guarantee that the property would not be overwritten by the initial parent?
> 
> 
> Br,
> Adriano
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml



More information about the Qt-qml mailing list