现有A界面中通过loader 加载B界面 ,A界面信号改变触发B界面函数调用
例子 main.qml 及A 界面
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
Window {
id: "aTest"
visible: true
width: 640
height: 480
title: qsTr("Hello World")
signal testSig();
Loader
{
id: "child"
width: 300
height: 200
source: "B.qml"
}
Button
{
text:"parent"
onClicked:{
console.log("parent clicked");
testSig()
}
}
}
B.qml
import QtQuick 2.0
import QtQuick.Controls 1.4
Item {
function test()
{
console.log("test be called")
}
Rectangle
{
width: 800
height: 600
color: "red"
Connections
{
target: aTest
onTestSig:
{
console.log("onTestSig")
test()
}
}
Button
{
x:100
text: "test"
onClicked: {
console.log("button clicked");
}
}
}
}
注意: function test() 函数应该放在界面的最外层定义