Prevent “inherited” signal handlers from executing

前端 未结 2 1828
孤独总比滥情好
孤独总比滥情好 2021-01-14 05:35

Defining a signal handler in a \"base\" component is pretty nifty when that functionality is going to be frequently used by many derived components.

However, in QML

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-14 05:55

    As peppe mentioned, one solution would be to instead of installing the handler directly, just make the handler a call to a function that can be overriden. However, function overriding itself is a mixed bag, when there is intent to reuse base implementations in the derived components, not necessarily in the order handlers stack with component inheritance.

    I actually came up with a flexible, albeit a little clunky solution. It is to manually disconnect the previous installed handler and manually connect a new one. This has two implications:

    1. The handlers cannot be anonymous expressions, they have to implemented as functions so they can be referenced for disconnect.

    2. The handlers cannot be bound using the declarative syntax (onSignal: handler()), as this doesn't connect to the handler function, but to an anonymous expression which invokes the handler function. So you can't disconnect.

    So it looks something like this:

    //BaseComp.qml
    QtObject {
        signal sig(int i)
        function baseHandler(i) {...}
        Component.onCompleted: sig.connect(baseHandler)
    }
    
    //DerivedComp.qml
    BaseComp {
        function derivedHandler(i) {...}
        Component.onCompleted: {
            sig.disconnect(baseHandler)
            sig.connect(derivedHandler)
        }
    }
    

    The basic pattern is disconnect the previous base handler in every derived component which overrides it. This way you get to access the base handlers from the derived components if there is a need to do that, if instead there is only one overridden handler function, the base implementations will not be accessible from the derived classes due to how overriding is implemented in QML (there will be two identically named functions as members of the object, but both of them will refer to the derived component override).

    It would be niceand useful if QML provided a pretty way to make a "unique" binding - something that purges all previous connections before making the new one. Then all that workaround code would not be needed.

提交回复
热议问题