Creating an Alias for a QML Type Member



  • I was wanting to create a Button Component with a fixed background and a variable label. Something like:
    @
    Button {
    property alias buttonLabel: styleLabel.label
    style: ButtonStyle {
    id: styleLabel
    label: Row { }
    background: Rectangle { ... }
    }
    }
    @

    Is it possible to create a property alias for "label"? I can't seem to find the correct syntax to do this. Thanks!



  • You probably need to make alias for Row's id or something inside Row



  • What is the issue you are facing. What you are using the correct syntax.


  • Moderators

    bq. Is it possible to create a property alias for “label”?

    I guess it won't work as the Component which it refers to would not have been initiated.
    If your purpose is to dynamically change the label's component you can create a property Component and the assign it a Component when you call it.
    eg:
    @
    MyButton.qml

    Item {
    property Component myComp
    Button {
    style: ButtonStyle {
    id: styleLabel
    label: myComp
    background: Rectangle { ... }
    }
    }
    }

    Main.qml

    MyButton {
    myComp: Qt.createComponent("labelDelegate1.qml")
    }

    MyButton {
    myComp: Qt.createComponent("labelDelegate2.qml")
    }
    @

    Hope this helps...


Log in to reply
 

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