2017-10-17 323 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; 
      } 
     } 
    } 
}