[PyQt] Hijacking QtCore.connect, disconnect and emit
Matt Newell
newellm at blur.com
Thu Jul 12 22:55:53 BST 2007
On Thursday 12 July 2007 14:40, Carlos Scheidegger wrote:
> Hello,
>
> 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""" @staticmethod
> def call(*args):
> callableObject(*args)
> _oldConnect(*args)
> return call
>
> def _wrapDisconnect(callableObject):
> """Returns a wrapped call to the old version of
> QtCore.QObject.disconnect""" @staticmethod
> def call(*args):
> callableObject(*args)
> _oldDisconnect(*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,
> -carlos
Qt has builtin callbacks for signal slot debugging. I think you could use
python's ctypes module to create python callbacks that would work. I don't
have much experience with ctypes or using Qt's callbacks, so I can't provide
any further details.
Matt
More information about the PyQt
mailing list