【问题标题】:QTimer::singleShot equivalent for QMLQTimer::singleShot 等效于 QML
【发布时间】:2015-08-25 14:11:49
【问题描述】:

考虑这个 C++ 语句(例如 from docs):

QTimer::singleShot(600000, &app, SLOT(quit()));

如何在 .qml JavaScript 中做同样的事情,像这样的 QML:

Rectangle {
    property int counter: 0
    onCounterChanged: {
        if (counter > 42) {
            // do equivalent of above C++ statement here
        }
    }
    // more code, which actually manipulates counter 
}

有一个明显的解决方案是拥有单独的Timer,然后由这段 JavaScript 代码启动,如果单行不可行,我会接受它作为答案。是吗?

【问题讨论】:

  • 相当于Timer。设置repeat: false 以获得单发行为。
  • repeat 实际上默认是 false
  • 好问题。 setTimeout/setInterval 是不可能的,因为它们无法使用。您可以将this answer 视为一种 - 相当老套的 - 方法。通过在函数签名中设置参数,您可以将使用减少到一行,例如delay(/*repeat*/ true, 12000, functionName).

标签: qt qml


【解决方案1】:

将 Timer 对象的“重复”属性更改为 false。

import QtQuick 1.0

Item {
    Timer {
        id: timer
        interval: 600000
        running: false
        repeat: false
        onTriggered: Qt.quit()
    }

    Rectangle {
        property int counter: 0
        onCounterChanged: {
            if (counter > 42) {
                timer.running = true
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    我最终将它添加到我的 main.qml:

    Component {
        id: delayCallerComponent
        Timer {
        }
    }
    
    function delayCall( interval, callback ) {
        var delayCaller = delayCallerComponent.createObject( null, { "interval": interval } );
        delayCaller.triggered.connect( function () {
            callback();
            delayCaller.destroy();
        } );
        delayCaller.start();
    }
    

    可以这样使用:

    delayCall( 1000, function () { ... } );
    

    【讨论】:

      【解决方案3】:

      下面是使用SequentialAnimation 元素的方法:

      SequentialAnimation {
          id: quitTimer
          PauseAnimation { duration: 60000 }
          ScriptAction { script: Qt.quit() }
      }
      
      Rectangle {
          property int counter: 0
          onCounterChanged: {
              if (counter > 42) {
                  quitTimer.start()
              }
          }
      }
      

      如果太难看,就用它做一个组件:

      // SingleshotTimer.qml
      import QtQuick 2.0
      SequentialAnimation {
          property alias delay: delayAnim.duration
          property alias script: scriptAction.script
      
          PauseAnimation { id: delayAnim; duration: 10000 }
          ScriptAction { id: scriptAction }
      }
      

      使用这个新组件可以满足您的需求:

      SingleshotTimer { id: timer; delay: 60000; script: Qt.quit() }
      
      Rectangle {
          property int counter: 0
          onCounterChanged: {
              if (counter > 42) {
                  timer.start()
              }
          }
      } 
      

      【讨论】:

        【解决方案4】:

        QML中有一个定时器组件

        import QtQuick 2.0
        
        Item {
            Timer {
                interval: 500; running: true; repeat: true
                onTriggered: time.text = Date().toString()
            }
        
            Text { id: time }
        }
        

        更多详情see the documentation

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多