QML用计时器移动文本

Har*_*ris 3 qt qml

我需要在屏幕上从右到左连续创建移动文本,我已经使用QML TimerText元素实现了它.

下面的代码工作正常,但我担心下面的代码导致更多的CPU或内存使用主要是因为定时器每33毫秒触发一次.我必须在我的应用程序和多个实例中使用它,例如在许多网格窗口内.

这是正确的方法吗?或者有什么比这更好的吗?

Rectangle{
        width:parent.width
        height:parent.height
        color: "#333333"

        Timer {
            id: resetTimer
            interval: 33
            repeat: true
            running: true
            onTriggered: {
             console.log("triggred");
             moving_text.x = moving_text.x-1
             if(moving_text.x<-1*moving_text.paintedWidth)
               moving_text.x=parent.width
            }
        }

        Text{
            id:moving_text
            x:parent.width
            text:"Moving text"
            color: "white"
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ans*_*mar 6

为什么让事情变得如此复杂.您可以使用NumberAnimationx,如下所示:

import QtQuick 2.0

Rectangle{
    id: root
    width:250
    height:250
    color: "#333333"

    Text{
        id:moving_text
        x:parent.width
        text:"Moving text"
        color: "white"

        NumberAnimation on x{
            from: root.width
            to: -1*moving_text.width
            loops: Animation.Infinite
            duration: 3000
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

至于你对内存和CPU使用的关注,你应该比较两种方法并检查哪一种适合你.但我的个人建议是使用NumberAnimation.