简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM