如何在控制台上打印通过NumberAnimation更改的值?

时间:2013-12-11 10:05:35

标签: qt qml qt-quick

import QtQuick 1.0

Rectangle 
{
   id: rect
   width: 100; height: 100
   color: "red"
   property int hh:0

   Text
   {
     text: hh
   }

   MouseArea 
   {
    anchors.fill: parent

    onPressed:
    {
        animation.start()
        console.log(hh)
    }

    NumberAnimation 
    { 
        id: animation
        target: rect
        property: "hh"
        to: 50; 
        duration: 1000 
    }
  }
}

控制台上没有打印任何内容。 我知道hh正在改变,但我希望同时在控制台上看到它的打印值。该怎么办?

1 个答案:

答案 0 :(得分:3)

您的值hh在控制台上实际上是关联的,但只有一次,当按下鼠标时。因此,它第一次打印0(因为动画没有更改值并且是异步的),而其他时间都是50。

如果要打印hh的所有中间值,只需处理此属性的更改信号:

import QtQuick 1.0

Rectangle 
{
   id: rect
   width: 100; height: 100
   color: "red"
   property int hh:0

   Text
   {
     text: hh
   }

   MouseArea 
   {
    anchors.fill: parent

    onPressed:
    {
        animation.start()
        console.log(hh)
    }

    NumberAnimation 
    { 
        id: animation
        target: rect
        property: "hh"
        to: 50; 
        duration: 1000 
    }
  }

  onHhChanged: console.log(hh)
}

为了完整性,请注意用于自定义自定义属性上的信号句柄的 camelize 语法约定。

相关问题