I'm trying to make alias of a component contained inside a tab element but without success. Even this small example does not work :
Tab
{
id: tab1
title:'tab1'
property alias tab1Text: test.text
Text {
id:test
text:'Test123'
}
}
I got this error :
property alias tab1Text: test.text
Can somebody indicate me where is the issue and how can I fix it ?
The error occurs since the target of your alias declaration is not valid.
In QML you need to distinguish between Object and Component and what you have is not always transparent to the user.
Sometimes you can find in the documentation a property like sourceComponent
If you assign something to it, like:
the
Item
won't be instantiated. Instead aComponent
for thisItem
is implicitly created that can then be used to createItem
s. But since there is no instance of theItem
yet, you also can't access any properties inside this non-existentItem
.Then in QML you have something called
default property
.This property is the place to which all the subobjects are automatically assigned to.
and
are equivalent. In most cases the default property is not of type
Component
, so the assigned thing is directly instantiated.Now how to solve the problem? You might set
active: true
and read or write totheTab.item.theProperty
using bindings, but you still can't alias to it since the syntax for alias only allowsid.property
and notid.someProperty.someNestedProperty
.But setting
active: true
implies that not only the properties are created, but all visual items, which is usually not desired. In my opinion, the best way is, to keep data and visual representation separated from each other:You can have a ViewModel in which you have all the data stored, and you use the visual representation to bind to it. So the binding is initiated by the
Tab
to read (and possibly modify) thetext
. And your other object does the same, to the same property of the model object.A model is not necessarily some sort of list of objects. It might be just one object with various properties for your view to display.
You can create a string property in
Tab
and bindText.text
to it. Binding is created when Tab's dynamic content is loaded. No need to force loading of tabs withactive
property.You can't alias to a direct child of the
Tab
since its content is not loaded by default:What you can do is using a property on
item
instead of an alias:But still it will be bound to the actual text only if the content is loaded (current Tab or with
active: true
).You can also add an
Item
around your content:But still the alias will not be available outside of this Item (including at the root of the tab).