Set attached property in function



  • In QML is it possible to set an attached property from a function? For example Layout.maximumHeight. If I have a function.

    function setMaxHeight()
    {
        //Can I set Layout.maximumHeight on a component here?
    }
    

  • Moderators

    @krobinson Hi! That's easy:

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.2
    import QtQuick.Controls 1.4
    
    Window {
        visible: true
        width: 600
        height: 400
        color: "gray"
    
        function myFunction(x) {
            myItem.Layout.preferredWidth = x
        }
    
        RowLayout {
            anchors.fill: parent
            Rectangle {
                color: 'orange'
                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.minimumWidth: 50
            }
            Rectangle {
                id: myItem
                color: 'plum'
                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.minimumWidth: 50
            }
        }
    
        Row {
            Button {
                text: "Click me!"
                onClicked: myFunction(300)
            }
            Button {
                text: "Close"
                onClicked: Qt.quit()
            }
        }
    }
    


  • Thanks, I swear I tried that. That's nice and easy.


Log in to reply
 

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