Set attached property in function
Solved
QML and Qt Quick
-
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? }
@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() } } }