[PyQt] Hijacking QtCore.connect, disconnect and emit

Carlos Scheidegger cscheid at sci.utah.edu
Thu Jul 12 22:40:49 BST 2007


	I am a developer of a largish (~75kloc) application that uses PyQt, and I'm
trying to track down a bug that might be related to signal handling. I was
hoping to be able to track down every signal that is emitted, even if that
makes everything superslow while debugging. I have managed to hijack PyQt's
connect and disconnect calls by a seriously horrible hack:

_oldConnect = QtCore.QObject.connect
_oldDisconnect = QtCore.QObject.disconnect

def _wrapConnect(callableObject):
    """Returns a wrapped call to the old version of QtCore.QObject.connect"""
    def call(*args):
    return call

def _wrapDisconnect(callableObject):
    """Returns a wrapped call to the old version of QtCore.QObject.disconnect"""
    def call(*args):
    return call

def enableSignalDebugging(**kwargs):
    """Call this to enable Qt Signal debugging. This will trap all
    connect, and disconnect calls."""

    f = lambda *args: None
    connectCall = kwargs.get('connectCall', f)
    disconnectCall = kwargs.get('disconnectCall', f)
    emitCall = kwargs.get('emitCall', f)

    def printIt(msg):
        def call(*args):
            print msg, args
        return call
    QtCore.QObject.connect = _wrapConnect(connectCall)
    QtCore.QObject.disconnect = _wrapDisconnect(disconnectCall)

Strangely enough, this works. However, the direct extension of the trick to
QtCore.emit does not. Is there some anything I can do (even if it is similarly
hideous) to hijack the call to emit?

We're currently using PyQt 4.1, sip 4.5.

Thanks a lot in advance,

More information about the PyQt mailing list