QtQuick动态创建组件

1.Qt.createComponent 创建组件对象 createObject指定父元素

**Ani.Qml
AnimatedImage {


    id: animation; source: "qrc:/T.gif"


    width: 40
    height: 40
}
**Text.Qml
Text{
    width: 40
    height: 40
    text: "Hello"
    verticalAlignment: Text.AlignBottom
}
**main.Qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    property var ff: 0
    SwipeView {
        id: swipeView
        anchors.fill: parent
        currentIndex: tabBar.currentIndex

        Page{

            Flow{
                id:grid 
                spacing: 0
                width: swipeView.width
                height: swipeView.height

                Button{
                    text: "hello"
                    onClicked: {
                        ff++;
                        if(ff%2==0)
                        {
                            loadButton()
                        }else{
                            loadImg()
                        }


                    }

                    function loadButton() {
                        var obj = Qt.createComponent("Text.qml");
                        if (obj.status == Component.Ready) {
                            var txt = obj.createObject(grid); 
                        }
                    }


                    function loadImg() {
                        var obj = Qt.createComponent("Ani.qml");
                        if (obj .status == Component.Ready) {
                            var img = obj .createObject(grid);
                        }
                    }
                }
                Button{
                    text: "hello"
                }
                Button{
                    text: "hello"
                }
                Button{
                    text: "hello"
                }
                Button{
                    text: "hello"
                }

            }


            MouseArea{
                onClicked: {
                    parent.loadButton()
                }
            }
        }


    }

    footer: TabBar {
        id: tabBar
        currentIndex: swipeView.currentIndex
        TabButton {
            text: qsTr("First")
        }
        TabButton {
            text: qsTr("Second")
        }
    }
}

这里写图片描述

简化:Gobal.js

**Gobal.js
.pragma library
var Component_Ready =1;
function  createxx(file,parent)
{
    var component = Qt.createComponent(file);
    if (component.status == Component_Ready) {
        var button = component.createObject(parent);
        return  button;
    }

    return null;
}
***********
import "Gobal.js"   as  Helper
  function loadButton() {
                      var obj =   Helper.createxx("Button.qml",grid);
                         if(obj!=null)
                        {
                            obj.color ="red"
                        } 
                    }

2.使用createQmlObject创建,不建议使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值