2种:组件在外部文件 和 在本文档中
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.qml
import QtQuick 2.0
Item {
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 {
id:com
Rectangle{
property var col: "red"
color: col;
}
}
Component {
id:com2
Button{
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_;
}
}