0

When I define a Component in QML I can define a alias property to expose properties of QtObjects that are otherwise hidden by the Components root.

Especially interesting is the default property alias to build powerful reusable and adaptable Components. But there is no generic way to have alter the nameresolution, which is bad for properties and signals.

Example: MyComp1.qml

Item { id: rootItem default property alias content: childItem.data signal sig() Item { id: childItem } } 

main.qml

MyComp { id: myObj signal sig1() onSig1: console.log('here') Button { id: button onClicked: { parent.sig() parent.sig1() } } } 

Here button is now a child of childItem, but both signals: sig() and sig1() are defined in rootItem, which leads to an error. To the user of of my nicely designed Component it is not transparent. He would need to look in the code of the Component to realize, why the buttons parent is not myObj/rootItem but in fact of childItem. This might lead to bugs.

Therefore I am looking for a way to prevent it.
For signal sig() this would be possible by the modifying the MyComp and manually forward the signal from childItem to rootItem:

Item { id: rootItem default property alias content: childItem.data signal sig() Item { id: childItem signal sig() onSig: rootItem.sig() } } 

But is there a way to shortcut this, so that the user does not need to us myObj.sig1() but might use parent.sig1().

Same goes for properties, that are added uppon instantiation:

MyComp { signal sig1() property int num: 5 Button { text: parent.num // <--- Error } } 

It would be nice, if the properties and signals defined uppon instantiation would be available as parent.propertyName to the Objects added by a default-property aswell, I think. Is there a way (to maybe hack this)?

1
  • I really struggled with the title of this question. If you have a better suggest it via edit or comment please. Commented Mar 16, 2017 at 13:54

1 Answer 1

0

There is no way to do this.

What you can do is stop assuming the outer scope in QML code is always the parent.

You could either access it directly by id:

MyComp { id: myComp signal sig1() property int num: 5 Button { text: myComp.num onClicked: myComp.sig1() } } 

Or if the object you want to access is the root item, you can access its properties implicitely:

MyComp { signal sig1() property int num: 5 Button { text: num onClicked: sig1() } } 

One good practice (albeit maybe too extreme for most case) that can be deduced from this situation, is to only use parent when you actually want to access the parent Item in the visual hierarchy, for things like anchors or width and height.

Sign up to request clarification or add additional context in comments.

1 Comment

That is sad :-(. I myself am (trying) to follow this good practice already. But for reusable components it would have been nice to have something like: default context: someID that you could - but not must - set to the same object you reference with default property alias blub: someID.data or similar.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.