2015-01-17 18 views
5

我有一個基類的項目是這樣的:如何覆蓋超分量的信號處理

基地 .qml:

Item { 
    Thing { 
     id: theThing; 

     onMySignal: { console.log("The signal"); } 
    } 
} 

我試圖做一個衍生項目 - Derived.qml

如何覆蓋theThingonMySignal處理程序?我曾嘗試這樣的東西......

衍生 .qml:

Base { 
    theThing.onMySignal: { console.log("Do different things with theThing in Derived") } 
} 

,但我無法找到任何東西來告訴我如何正確語法表達這個,或者是否/如何真正去關於它!

回答

4

可以在超類定義的信號處理程序的代碼作爲屬性和衍生產品覆蓋它:

Item { 
    property var handlerCode: function() { console.log("In Superclass") }; 

    Thing { 
     id: theThing; 

     onMySignal: handlerCode() 
    } 
} 

重寫:

Base { 
    handlerCode: function() { console.log("In Derived Item!") }; 
} 
+0

真棒,謝謝! – GreenAsJade

+0

甚至不需要添加屬性...只需要在base中定義一個函數,就可以在衍生中使用相同名稱創建一個新的函數來覆蓋它...所以只需在Base中執行'onSignal:callBackFunction()',在Base和你想要的派生組件中定義簡單的'callBackFunction()' –