不管是component还是分文件编写的一个自定义组件,遵循如下:
a:不管哪一级的子对象 都可以直接调根节点下的方法和属性
b:不管哪一级的子对象 调用非根节点的方法和属性,前面要加上该节点的id,即id.方法()或id.属性的形式
import QtQuick 2.0
import QtQuick.Controls 2.12
Rectangle{id:yux:15y:135width:150height: 100color: "green"property string txt: "xuhaitao"function jiafa(a ,b){btn02.xy02();return a+b}function da(){console.log("da ...")}Button{id:btn01width:100height: 50text:txtfunction xy01(){console.log("btn01 xy01 done...")}Button{id:btn02x:180width:50height: 50text:txtfunction xy02(){btn01.xy01();da();console.log("btn02 xy02 done...")}}}}
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick 2.7
import QtQuick.Layouts 1.3Window {visible: truewidth: 640height: 480title: qsTr("Hello World")HaHa{id:rect01}Button{id:btn01text: "dian ji"x:100onClicked: {console.log(rect01.jiafa(18,91))rect01.txt="xs"}}
}
运行结果:
FR:徐海涛(hunkxu)