2017-10-17 46 views
0

我的窗口中有一个文本元素,我希望它每眨眼一秒钟或几秒钟就会闪烁或出现并消失。如何使QT文本每隔几毫秒重新出现(闪烁)

我的代码是:

import QtQuick 2.6 
import QtQuick.Window 2.2 

Window { 
    visible: true 
    width: 640 
    height: 480 
    title: qsTr("Hello World") 

    Text { 
     id: my_text 
     text: "Hello" 
     font.pixelSize: 30 
    } 
} 

回答

3

的任务很容易与Timer解决。

import QtQuick 2.6 
import QtQuick.Window 2.2 

Window { 
    visible: true 
    width: 640 
    height: 480 
    title: qsTr("Hello World") 

    Text { 
     id: my_text 
     font.pixelSize: 30 
     text: "Hello" 
    } 

    Timer{ 
     id: timer 
     interval: 1000 
     running: true 
     repeat: true 
     onTriggered: my_text.opacity = my_text.opacity === 0 ? 1 : 0 
    } 
} 
+0

为什么你使用'opacity',而不是'visible'?恕我直言'可见=!可见'更容易阅读。使用不透明度有什么性能好处? – derM

0

另一种解决方案使用OpacityAnimator

import QtQuick 2.6 
import QtQuick.Window 2.2 

Window { 
    visible: true 
    width: 640 
    height: 480 
    title: qsTr("Hello World") 

    Text { 
     anchors.centerIn: parent 
     id: my_text 
     text: "Hello" 
     font.pixelSize: 30 

     OpacityAnimator { 
      target: my_text; 
      from: 0; 
      to: 1; 
      duration: 400; 
      loops: Animation.Infinite; 
      running: true; 
      easing { 
       type: Easing.InOutExpo; 
      } 
     } 
    } 
}