9

I have a base class item something like this:

Base.qml:

Item {
    Thing {
      id: theThing;

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

And I'm trying to make a derived item - Derived.qml.

How can I override the onMySignal handler of theThing? I have tried stuff like this...

Derived.qml:

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

but I can't find anything to tell me how to express this syntactically correctly, or whether/how to actually go about it!

Nejat
  • 31,784
  • 12
  • 106
  • 138
GreenAsJade
  • 14,459
  • 11
  • 63
  • 98

1 Answers1

9

You can define the code of the signal handler as a property in superclass and override it in the derived item:

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

    Thing {
      id: theThing;

      onMySignal: handlerCode() 
     }
}

Overriding :

Base {
    handlerCode: function () { console.log("In Derived Item!") };
}
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • 3
    Or even without adding a property... Just having a function defined in base, you can just create a new one in derived with the same name to override it... So just do `onSignal: callBackFunction()` in Base and define the simple `callBackFunction()` in both Base and the derived components you want – Treviño Sep 22 '16 at 10:26