how to disconnect the method for a signal in qml?

前端 未结 2 913
独厮守ぢ
独厮守ぢ 2021-01-06 16:30

In QML, in some situations, I need to remove the old method to a signal and redefine a new method to handle the signal, a demo is as following:

import QtQuic         


        
相关标签:
2条回答
  • 2021-01-06 16:42

    You haven't used the explicit connect() function to begin with. To disconnect() a function, it has to have been connected explicitly using connect().

    There is an example here that might help:

    http://doc.qt.io/qt-5/qtqml-syntax-signals.html#connecting-signals-to-methods-and-signals

    0 讨论(0)
  • 2021-01-06 16:47

    You can't disconnect the default signal handler onSignal: { do what ever }.

    You can only dissconnect, when you have connected with onSignal.connect(say) beforehand. You could do this in Component.onCompleted.
    If you use the syntax:

    onSignal: say()
    

    this equals

    onSignal.connect(function() { say() })
    

    and therefore you can't use onSignal.disconnect(say) as say was never connected. (I don't know whether it is connected to onSignal or to signal - both could be done manually, or whether it is not connected at all. You can't disconnect it, without having a reference to the implicitly created function.)


    In QML, the nicer way would be to stay declarative. For this you can use the Connections-construct:

    Connections {
        target: mouse // set to null to disconnect (Qt<=5.6)
        onClicked: { do what ever }
        enabled: true // change this, when you want to disconnect it (Qt>=5.7)
    } 
    
    0 讨论(0)
提交回复
热议问题