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()  函數應該放在界面的最外層定義


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章