【问题标题】:Qt NumberAnimation property binding loop problemQt NumberAnimation 属性绑定循环问题
【发布时间】:2020-10-07 12:54:15
【问题描述】:

我是 Qt QML 的新手。我正在使用 JS 动态创建 numberAnimation 但收到绑定循环警告。

qrc:/main.qml:18:9:QML 项目:检测到属性“anime”的绑定循环

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    id : global
    width: 550
    height: 550
    visible: true
    title: qsTr("Hello World")

    Rectangle{
        id : exp
        x : 0
        y : 0
        color : "red"
        width : 50 ; height : 50

        property var anime : createAnimation(exp);

        function createAnimation(parent){
            let numAnime = Qt.createQmlObject("import QtQuick 2.12; NumberAnimation { onStopped:{to = Math.random()*500; restart() }}", parent)
            numAnime.duration = 500
            numAnime.easing.type = Easing.OutInSine
            numAnime.target = parent
            numAnime.property = "x"
            numAnime.running = true
            return numAnime
        }
    }
}

另外,有没有办法在 createAnimation(args) 函数中定义 onStopped body?

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    您可以通过在Component.onCompleted 中分配来删除anime 上的绑定。是的,您可以在 createAnimation 的主体中定义 onStopped,方法是将其连接到一个函数,如下所示:

        Rectangle{
            id : exp
            x : 0
            y : 0
            color : "red"
            width : 50 ; height : 50
    
            property var anime
            Component.onCompleted: anime = createAnimation(exp);
    
            function createAnimation(parent){
                let numAnime = Qt.createQmlObject("import QtQuick 2.12; NumberAnimation {}", parent)
                numAnime.duration = 500
                numAnime.easing.type = Easing.OutInSine
                numAnime.target = parent
                numAnime.property = "x"
                numAnime.running = true
                numAnime.onStopped.connect(function() { numAnime.to = Math.random() * 500; numAnime.restart() })
                return numAnime
            }
        }
    

    【讨论】:

    • 这正是我想要的。但是你能解释一下为什么首先会有绑定循环吗?
    • 其实我也不确定原因,没有。
    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    相关资源
    最近更新 更多