Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Accessing custom properties by alias
QtWS25 Last Chance

Accessing custom properties by alias

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 2 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DuBu
    wrote on 2 Nov 2017, 09:41 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • O Offline
      O Offline
      ODБOï
      wrote on 13 Nov 2017, 09:07 last edited by
      #2

      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

      D 1 Reply Last reply 13 Nov 2017, 09:15
      1
      • O ODБOï
        13 Nov 2017, 09:07

        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

        D Offline
        D Offline
        DuBu
        wrote on 13 Nov 2017, 09:15 last edited by
        #3

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

        1 Reply Last reply
        1
        • O Offline
          O Offline
          ODБOï
          wrote on 13 Nov 2017, 15:51 last edited by ODБOï
          #4

          @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

          D 1 Reply Last reply 13 Nov 2017, 16:17
          0
          • O ODБOï
            13 Nov 2017, 15:51

            @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

            D Offline
            D Offline
            DuBu
            wrote on 13 Nov 2017, 16:17 last edited by
            #5

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

            1 Reply Last reply
            0
            • O Offline
              O Offline
              ODБOï
              wrote on 13 Nov 2017, 17:20 last edited by
              #6

              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

              D 2 Replies Last reply 13 Nov 2017, 18:09
              0
              • O ODБOï
                13 Nov 2017, 17:20

                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

                D Offline
                D Offline
                DuBu
                wrote on 13 Nov 2017, 18:09 last edited by
                #7

                @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"
                
                1 Reply Last reply
                0
                • O ODБOï
                  13 Nov 2017, 17:20

                  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

                  D Offline
                  D Offline
                  DuBu
                  wrote on 13 Nov 2017, 18:17 last edited by
                  #8

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

                  //    testAlias.propTxt: "TEST"
                      Binding { target: myCustomRec.testAlias; property: "propTxt"; value: "TEST" }
                  
                  1 Reply Last reply
                  1
                  • O Offline
                    O Offline
                    ODБOï
                    wrote on 13 Nov 2017, 18:45 last edited by
                    #9

                    @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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved