createComponent createObject

Sprite.qml

import QtQuick 2.6
Rectangle{
    width: 80;height: 50
    color: "blue"
}

componentCreation.js

var component;
var sprite;

function createSpriteObjects(){
    component=Qt.createComponent("Sprite.qml");
    if(component.status==Component.Ready)
        finishCreation();
    else
        component.statusChanged.connect(finishCreation);
}

function finishCreation(){
    if(component.status==Component.Ready){
        sprite=component.createObject(appWindow);
    if(sprite==null){
        console.log("Component.Error: ",sprite==null);}
    else{
        sprite.x=100;
        sprite.y=200;
    }
    }
    else if(component.status==Component.Error){
        console.log("Component.Error: ",component.errorString());
    }
}

main.qml

import QtQuick 2.6
import "componentCreation.js" as MyScript

Rectangle{
    id:appWindow
    width: 300;height: 300
    Component.onCompleted:MyScript.createSpriteObjects();
}

SelfDestroyingRect.qml:

import QtQuick 2.6
Rectangle{
    id:rect
    width: 80;height: 50
    color: "green"

    NumberAnimation on opacity {
        to:0
        duration:3000
        onRunningChanged: {
            if(!running){
                console.log("Destroying... ");
                rect.destroy();
            }
        }
    }
}

main.qml

Rectangle{
    id:container
    width: 300;height: 200
    Component.onCompleted:{
        var component=Qt.createComponent("SelfDestroyingRect.qml");
        for(var i=0;i<5;i++){
            var object=component.createObject(container)
            object.x=(object.width+10)*i;
        }
    }
}

createQmlObject
var newObject = Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "red"; width: 20; height: 20}',
                                     parentItem,
                                     "dynamicSnippet1");


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值