Loader文件内外加载 信号槽方法属性

来源:互联网 发布:mac更新战网agent卡住 编辑:程序博客网 时间:2024/06/05 10:02

2种:组件在外部文件 和 在本文档中

//加载其他文件中的组件 不需要声明称Component//1.用loader.item.属性 访问属性//2.loader.item.方法 访问方法//3.用loader.item.方法.connect(槽)连接信号        Item {            Loader{                id:loader;                width: 200;                height: 200;                source:"qrc:code/CommetOne.qml"                onLoaded: {                    loader.item.initok.connect(dook);                    loader.item.testfunc("123");                    console.log("load")                    item.col = "yellow";                }            }        }        function dook()        {            //清除上一个组件的内存             loader.source="";             loader.source="qrc:code/CommetOne.qml";            console.log('Link ok');        }
****CommetOne.qmlimport QtQuick 2.0Item {    signal  initok();    id:com    property var col: "red"    Rectangle{        anchors.fill: parent;        color: col;        Component.onCompleted: {            console.log("***********");            mm.start();        }        TextInput{            width: 100            height: 40        }    }    Timer{        id:mm        running: true;        repeat: true;        interval: 100;        onTriggered: {            console.log('dotime')           initok();        }    }    function  testfunc(x)    {        console.log(x);    }}***main.qml//加载外部文件中的组件   简易方式 适用于不动态切换组件            Loader{                id:loader;                width: 200;                height: 200;                sourceComponent:CommetOne{                    id:one                    col:"yellow";                    onInitok: {                        console.log("init");                        testfunc("hello.....");                    }                }                onLoaded: {                    console.log("load");                    loader.item.testfunc("jjj");                }            }
//加载本文件中的Component组件    Component {        id:com        Rectangle{             property var col: "red"            color: col;        }    }    Component {        id:com2        Button{            //设置尺寸无用  Component中的第一个元素作为主体,尺寸会被loader中的覆盖            width: 20;            height: 20;             property var col: "red"            text:"hello";        }    }    Item {        Loader{            id:loader;            width: 200;            height: 200;            sourceComponent:com;            onLoaded: {                console.log("load");            }        }    }    Timer{        id:mm        running: true;        repeat: true;        interval: 1000;        property bool t_: false;        onTriggered: {            console.log('dotime');            loader.sourceComponent =null;            if(t_==true)            {                loader.sourceComponent =com2;                 loader.item.col = Material.color(Material.Orange);            }else{                loader.sourceComponent =com;                 loader.item.col = Material.color(Material.Blue);            }            t_ = !t_;        }    }
0 0