QML NumberAnimation:setRunning()不能用于非根动画节点

时间:2018-02-20 23:37:41

标签: qt popup qml

我的代码在这里:

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.

1 个答案:

答案 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()时,转换就会运行。