简体   繁体   English

如何在QML中延迟JavaScript动作?

[英]How to delay JavaScript action within QML?

I am building a C++ application based on QML . 我正在构建基于QMLC ++应用程序

To make it simple : 简单起见:

In my main QML file, I have a button (Rectangle) calling a JavaScript function (defined in an external JS file) when clicked: 在我的主要QML文件中,单击时有一个按钮(矩形)调用JavaScript函数(在外部JS文件中定义):

// My JS file linked to the main QML window
[...]
function actionOnButtonClicked()
{
    var x = 0;
    var y = 0;
    for(var i = 0; i < 3; i++)
    {
        createObject(x, y);
        x = x + 10;
        y = y + 10;
    } 
}

As you can see, in this function, I call n (= 3 here) times another JS function to dynamically create several QML objects to add to the scene: 如您所见,在此函数中,我调用n (这里= 3)乘以另一个JS函数来动态创建多个 QML对象以添加到场景中:

function createObject(xPosition, yPosition)
{
    component = Qt.createComponent("Symbol.qml");
    component.createObject(windowApp, {"x": xPosition, "y": yPosition});
}

This is working fine. 一切正常。 But the created object (Symbol) appears in the windowApp with a translation animation (around 1sec.), and I would like to wait for the first object's animation to complete before creating the second one... 但是创建的对象(Symbol)带有翻译动画(大约1秒)出现在windowApp中,我想在创建第二个对象之前等待第一个对象的动画完成...

As we cannot use setTimeOut() JavaScript function in QML, I wonder how I could achieve this. 由于我们无法在QML中使用setTimeOut() JavaScript函数,因此我想知道如何实现这一点。 I do not see how I could make use of the QML Timer object or even PauseAnimation... 我看不到如何利用QML Timer对象甚至PauseAnimation ...

Does somebody know how to add a delay between 2 QML JavaScript operations ? 有人知道如何在2个QML JavaScript操作之间增加延迟吗?

I think this QML Timer type can help you achieve what you want. 我认为这种QML Timer类型可以帮助您实现所需的功能。

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

       Text { id: time }
} 

You could probably do it so that you only create one "Symbol" from your button action and trigger a new symbol on some event in the new object. 您可以这样做,以便仅通过按钮操作创建一个“符号”,并在新对象中的某个事件上触发一个新符号。 Perhaps the animation ending triggers an event that you could use ? 也许动画结尾会触发您可以使用的事件?

Its been a while, I have missed QML. 已经有一段时间了,我错过了QML。 But let me try to suggest a solution. 但是,让我尝试提出解决方案。 I guess this might work, if you are calling that translationAnimation.running = true in Component.onComlpeted event. 我猜这可能行得通,如果您在Component.onComlpeted事件中调用那个translationAnimation.running = true I have posted a stupid answer before. 我以前发布过一个愚蠢的答案。 Now I replace it with a lazy/ugly way to do this. 现在,我用一种懒惰/丑陋的方式替换它。 This is probably not the right way to do it, though this code may help your use case. 尽管此代码可能会对您的用例有所帮助,但这可能不是正确的方法。

CreateObject.js CreateObject.js

.pragma library

var objects = null;
var objectCount = 0;
var i = 0;
var mainWin;
var x = 0;
var y = 0;

function calledOnbuttonAction(parentWindow)
{
    if(objects === null)
    {
        mainWin = parentWindow;
        x = 0;
        y = 0;
        objects = new Array();
        createObject(x,y);
    }

    else
    {
        if(x <= mainWin.width)
            x = x + 28;
        else
        {
            x = 0;
            if(y <= mainWin.height)
                y = y + 28;
            else
            {
                console.debug("Exceeded window area!")
                return;
            }
        }
        createObject(x,y);
    }

}

function createObject(xPos, yPos)
{
    i++;
    var component = Qt.createComponent("Object.qml");
    objects[objectCount++] = component.createObject(mainWin, {"x": xPos, "y": yPos});
}

main.qml main.qml

import QtQuick 1.1
import "CreateObjects.js" as CreateObject

Rectangle {
    id: mainWindow
    width: 360
    height: 360

    Text {
        text: qsTr("Click inside window")
        anchors.centerIn: parent
        font.pixelSize: 18
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            CreateObject.calledOnbuttonAction(mainWindow); //passing the parent window object
        }
    }

}

Object.qml //Symbol in your case Object.qml //您的情况下的符号

//The Symbol

import QtQuick 1.1
import "CreateObjects.js" as CreateObject
Rectangle {

    id: obj
    width: 25
    height: 25

    gradient: Gradient {
        GradientStop {
            position: 0
            color: "#d11b1b"
        }

        GradientStop {
            position: 1
            color: "#ea4848"
        }
    }

    property alias animationStatus: completedAnimation

    NumberAnimation {
        id: completedAnimation;
        target: obj;
        property: "opacity";
        duration: 800;
        from: 0;
        to: 1.0;
        onRunningChanged: {
            if(!running && CreateObject.i < 900) // Decrease or increase the value according to the number of objects you want to create
            {
                CreateObject.calledOnbuttonAction();
            }
        }
    }

    Component.onCompleted: completedAnimation.running = true;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM