Accessing custom properties by alias



  • 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?



  • Hi,
    Use property alias like this:
    PS : don't use reserved keywords as id; exemple 'id : text' No ! text is reserved, or ' id : width ' No

    Item {
    property alias myTxtAlias : textId.text
    property alias myPosAlias : textId.pos

    Text {
    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



  • @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.



  • @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' properties

    import QtQuick 2.0

    Rectangle {
    width: 500; height: 200
    color: "lightgray"
    property alias r : aliasedRec

    Row{
    spacing : 10

    Rectangle{
    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



  • @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.



  • 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.2

    Window {
    visible: true
    width: 640
    height: 480

    MyCustomRec{  // 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



  • @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"
    


  • @LeLev Also interesting: using Binding instead of direct binding works:

    //    testAlias.propTxt: "TEST"
        Binding { target: myCustomRec.testAlias; property: "propTxt"; value: "TEST" }
    


  • @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 :

    //main.qml

    Window {
    visible: true
    width: 640
    height: 480

    Component.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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.