qml的信号与槽

现有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()  函数应该放在界面的最外层定义

 
发布了206 篇原创文章 · 获赞 18 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/lvmengzou/article/details/105248681