我有一个固定大小的矩形,它应该始终附着在窗口垂直中心的底部。如果窗口高度足够小,使得矩形能够接触边框,我想更改矩形的锚点,使其粘在窗口的底部。
我使用状态实现了这一点:
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
id: window
width: 200
height: 480
minimumHeight: maxSize
visible: true
property int maxSize: 150
property bool centerTop: maxSize < (height / 2)
Rectangle {
id: rect
states: [
State {
name: "Centered"
when: centerTop
PropertyChanges {
target: rect
anchors.top: parent.verticalCenter
anchors.bottom: undefined
}
},
State {
name: "Bottom"
when: !centerTop
PropertyChanges {
target: rect
anchors.top: undefined
anchors.bottom: parent.bottom
}
}
]
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: 10
anchors.rightMargin: 10
height: …Run Code Online (Sandbox Code Playgroud) qml ×1