Accessing custom properties by alias
-
wrote on 2 Nov 2017, 09:41 last edited by
Hi all,
I want to access a custom property via an alias but it's not working and throws an error.
Here's a code snippet:comp1.qml:
Item { property alias text: text Text { id: text property int pos: 0 } }
main.qml
... Comp1 { text.text: "Hello world" text.pos: 1 }
Changing "text.text" works just fine but the line trying to change "text.pos" throws the following error:
Cannot assign to non-existent property "pos"
Is this a bug or a feature?
-
wrote on 13 Nov 2017, 09:07 last edited by
Hi,
Use property alias like this:
PS : don't use reserved keywords as id; exemple 'id : text' No ! text is reserved, or ' id : width ' NoItem {
property alias myTxtAlias : textId.text
property alias myPosAlias : textId.posText {
id: textId // dont use property name 'text' as id ... id : myId or id myText ! but not id:text !
property int pos: 0
}
}Comp1 {
myTxtAlias : "Hello world"
myPosAlias : 1
}
LA -
Hi,
Use property alias like this:
PS : don't use reserved keywords as id; exemple 'id : text' No ! text is reserved, or ' id : width ' NoItem {
property alias myTxtAlias : textId.text
property alias myPosAlias : textId.posText {
id: textId // dont use property name 'text' as id ... id : myId or id myText ! but not id:text !
property int pos: 0
}
}Comp1 {
myTxtAlias : "Hello world"
myPosAlias : 1
}
LAwrote on 13 Nov 2017, 09:15 last edited by@LeLev (Thanks for the hint to not use reserved keywords as ids. The code I posted was just an example, I usually use different names for id's.)
Yes, aliasing a single property explicitly is the usual way. But I just noticed, it works also with whole components. That's pretty cool and saves a lot lines of code. But unfortunately it doesn't work with "inline" properties.
-
wrote on 13 Nov 2017, 15:51 last edited by ODБOï
@Dubu what are 'inline' properties ? is "property int rad : 10 " inline prop. ?
Here ' property alias r : aliasedRec ' is aliasing the whole component (aliasedRec),
so ' aliasUserRec ' can use r to 'copy' propertiesimport QtQuick 2.0
Rectangle {
width: 500; height: 200
color: "lightgray"
property alias r : aliasedRecRow{
spacing : 10Rectangle{
id:aliasedRec
property int rad : 10 // is this 'inline'
// radius : rad
height : 50
width : 60
color : "blue"
}Rectangle{
id:aliasUserRec
height : r.height
width : r.width
color : r.color
radius : r.rad
}
}
}LA
-
@Dubu what are 'inline' properties ? is "property int rad : 10 " inline prop. ?
Here ' property alias r : aliasedRec ' is aliasing the whole component (aliasedRec),
so ' aliasUserRec ' can use r to 'copy' propertiesimport QtQuick 2.0
Rectangle {
width: 500; height: 200
color: "lightgray"
property alias r : aliasedRecRow{
spacing : 10Rectangle{
id:aliasedRec
property int rad : 10 // is this 'inline'
// radius : rad
height : 50
width : 60
color : "blue"
}Rectangle{
id:aliasUserRec
height : r.height
width : r.width
color : r.color
radius : r.rad
}
}
}LA
wrote on 13 Nov 2017, 16:17 last edited by@LeLev Yes, correct, with "inline property" I meant the term "property int rad: ...".
Oh, that's interesting: Your example actually works. In your example you access the alias "r" and its properties only from inside the qml file (component). But could you try to put your example code in a file and access "r.rad" from a different file? Like:// YourExample.qml: ... // main.qml: YourExample { r.color: "red" // should work r.rad: 1 // shouldn't work }
Only when accessing the aliased component from outside the file/component I get the mentioned error.
-
wrote on 13 Nov 2017, 17:20 last edited by
I have tested this :
// MyCustomRec.qml
import QtQuick 2.0
Item {
property alias testAlias : alisaedRec
Rectangle{
id:alisaedRec
property string propTxt : "Hello World !"
}
}//Main.qml
import QtQuick 2.6
import QtQuick.Window 2.2Window {
visible: true
width: 640
height: 480MyCustomRec{ // this will create red 50x50 rectangle id:myCustomRec testAlias.color: "red" testAlias.height: 50 testAlias.width: 50 } Text { id: exepleText anchors.centerIn: parent text: myCustomRec.testAlias.propTxt // this will output "Hello World !" }
}
LA
-
I have tested this :
// MyCustomRec.qml
import QtQuick 2.0
Item {
property alias testAlias : alisaedRec
Rectangle{
id:alisaedRec
property string propTxt : "Hello World !"
}
}//Main.qml
import QtQuick 2.6
import QtQuick.Window 2.2Window {
visible: true
width: 640
height: 480MyCustomRec{ // this will create red 50x50 rectangle id:myCustomRec testAlias.color: "red" testAlias.height: 50 testAlias.width: 50 } Text { id: exepleText anchors.centerIn: parent text: myCustomRec.testAlias.propTxt // this will output "Hello World !" }
}
LA
wrote on 13 Nov 2017, 18:09 last edited by@LeLev If you put the following line right under the line "testAlias.width: 50" in your example you'll be rewarded with the mentioned error:
testAlias.propTxt: "TEST"
-
I have tested this :
// MyCustomRec.qml
import QtQuick 2.0
Item {
property alias testAlias : alisaedRec
Rectangle{
id:alisaedRec
property string propTxt : "Hello World !"
}
}//Main.qml
import QtQuick 2.6
import QtQuick.Window 2.2Window {
visible: true
width: 640
height: 480MyCustomRec{ // this will create red 50x50 rectangle id:myCustomRec testAlias.color: "red" testAlias.height: 50 testAlias.width: 50 } Text { id: exepleText anchors.centerIn: parent text: myCustomRec.testAlias.propTxt // this will output "Hello World !" }
}
LA
wrote on 13 Nov 2017, 18:17 last edited by@LeLev Also interesting: using Binding instead of direct binding works:
// testAlias.propTxt: "TEST" Binding { target: myCustomRec.testAlias; property: "propTxt"; value: "TEST" }
-
wrote on 13 Nov 2017, 18:45 last edited by
@DuBu said in Accessing custom properties by alias:
If you put the following line right under the line "testAlias.width: 50" in your example you'll be rewarded with the mentioned error:
Yes, strange ^^ I dont realy know what is going on exactly. In fact you can't access " testAlias.propTxt" from itself but if you do this main :
Window {
visible: true
width: 640
height: 480Component.onCompleted: console.log(myCustomRec.testAlias.propTxt = "TEST" ) // here you can read and write ' testAlias.propTxt '
MyCustomRec{
id:myCustomRec
testAlias.color: "red"
testAlias.height: 100
testAlias.width: 100
// testAlias.propTxt // Error
}}
So you can still access it from js function.
LA