Qt 为什么加载时会触发动画
当窗口打开时,您可以看到矩形滑出。我将Qt 为什么加载时会触发动画,qt,qml,qt5,Qt,Qml,Qt5,当窗口打开时,您可以看到矩形滑出。我将y属性设置为父级高度,因此它最初应该在窗口之外。为什么要设置动画? 我猜这是因为父母的身高。可能是因为加载时parent.height不可用,并且它最初设置为0 我有下面的例子要复制: import QtQuick 2.9 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World")
y
属性设置为父级高度,因此它最初应该在窗口之外。为什么要设置动画?我猜这是因为父母的身高。可能是因为加载时
parent.height
不可用,并且它最初设置为0
我有下面的例子要复制:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
id: test;
y: parent.height;
states: [
State {
name: "slideOut"
PropertyChanges{
target: test;
y: parent.height;
}
},
State {
name: "slideIn"
PropertyChanges{
target: test;
y: 0;
}
}
]
Behavior on y {
NumberAnimation {
duration: 500;
}
}
color: "red";
width: parent.width;
height: parent.height;
}
MouseArea {
anchors.fill: parent;
onClicked: {
if(test.state == "slideIn") {
test.state = "slideOut";
} else {
test.state = "slideIn";
}
}
}
}
你的猜测听起来很准确 您应该改为与状态一起使用:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
id: test
y: parent.height
width: parent.width
height: parent.height
color: "red"
states: [
State {
name: "slideOut"
PropertyChanges {
target: test
y: parent.height
}
},
State {
name: "slideIn"
PropertyChanges {
target: test
y: 0
}
}
]
transitions: [
Transition {
NumberAnimation {
property: "y"
duration: 500
}
}
]
}
MouseArea {
anchors.fill: parent
onClicked: {
if (test.state == "slideIn") {
test.state = "slideOut"
} else {
test.state = "slideIn"
}
}
}
}
另一种解决方案是使用行为属性仅在窗口准备就绪时运行动画。我不确定你会把它建在哪一处。一些想法:
已启用:window.height>0
已启用:窗口处于活动状态
- 你的猜测听起来很准确
您应该改为与状态一起使用:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
id: test
y: parent.height
width: parent.width
height: parent.height
color: "red"
states: [
State {
name: "slideOut"
PropertyChanges {
target: test
y: parent.height
}
},
State {
name: "slideIn"
PropertyChanges {
target: test
y: 0
}
}
]
transitions: [
Transition {
NumberAnimation {
property: "y"
duration: 500
}
}
]
}
MouseArea {
anchors.fill: parent
onClicked: {
if (test.state == "slideIn") {
test.state = "slideOut"
} else {
test.state = "slideIn"
}
}
}
}
另一种解决方案是使用行为属性仅在窗口准备就绪时运行动画。我不确定你会把它建在哪一处。一些想法:
已启用:window.height>0
已启用:窗口处于活动状态