我通过绘制一个高度1的矩形来绘制一条线。然后,我有一个单击按钮,可以通过降低其y值来向上移动阵列。在达到一定的y值之后,我如何获得不移动的行?
Item
{
Rectangle
{
id: rectangle
color:"#ef5350"
y: 50
width: parent.width
height: 1
}
Button
{
anchors.centerIn: parent
text: "Move line up"
onClicked: rectangle.y-=10
}
}
我希望我的行不超过特定的y值。
使用属性管理偏移并将其绑定到最大/最小值:
Rectangle {
property int offset: 0
id: rectangle
color: "#ef5350"
y: 70 + Math.max(offset, -5 * 10) // Not more than 5 hits
width: parent.width
height: 1
}
Button {
anchors.centerIn: parent
text: "Move line up"
onClicked: rectangle.offset-=10
}
您也可以在onClicked
属性中移动逻辑:
onClicked: {
if (rectangle.offset == -50)
return
rectangle.offset-=10
}