我的代码在这里:
MyPopup.qml
:
import QtQuick 2.0
import QtQuick.Window 2.3
import QtQuick.Controls 2.2
Popup {
id: popup
width: 250
height: 250
closePolicy: Popup.NoAutoClose
y: -300
background: Rectangle {
anchors.fill: parent
color: '#3f3f3f'
opacity: 15
Text {
text: 'HELLO WORLD!'
color: 'white'
font.pointSize: 20
anchors.centerIn: parent
}
Button {
anchors.bottom: parent.bottom
text: 'Close Popup'
onClicked: popup.close()
}
}
enter: Transition {
NumberAnimation {
target: parent
property: 'y'
to: (Screen.height / 2) - (height / 2)
duration: 400
running: true
}
}
exit: Transition {
NumberAnimation {
target: parent
property: 'y'
to: -((Screen.height / 2) - (height / 2))
duration: 400
running: true
}
}
}
我在main.qml
中放了一个按钮。单击该按钮时,popup.open()
正在运行。我在MyPopup.qml
中放了一个按钮,当我点击该按钮时,popup.close()
正在运行。当我按下按钮时,我的应用程序正在冻结并关闭。
我收到此警告:
qrc:/MyPopup.qml:31:5: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.
qrc:/MyPopup.qml:41:5: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.
答案 0 :(得分:1)
在documentation中,给出了两个示例,其中没有一个实例化running
属性。 enter
属性的示例:
Popup {
enter: Transition {
NumberAnimation { property: "opacity"; from: 0.0; to: 1.0 }
}
}
对于exit
属性:
Popup {
exit: Transition {
NumberAnimation { property: "opacity"; from: 1.0; to: 0.0 }
}
}
我在没有running: true
行的情况下尝试了您的代码并且它有效。因此,当调用popup.open()
或popup.exit()
时,转换就会运行。