Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved Set attached property in function

    QML and Qt Quick
    2
    3
    555
    Loading More Posts
    • 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.
    • K
      krobinson last edited by

      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?
      }
      
      ? 1 Reply Last reply Reply Quote 1
      • ?
        A Former User @krobinson last edited by

        @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()
                }
            }
        }
        
        1 Reply Last reply Reply Quote 2
        • K
          krobinson last edited by

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

          1 Reply Last reply Reply Quote 0
          • First post
            Last post