Search code examples
qtinheritanceqmlhandlerqt-quick

How to override signal handler of superclass component


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!


Solution

  • 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!") };
    }