Debugging a pyQT4 app?

Paul Wicks picture Paul Wicks · Nov 15, 2009 · Viewed 13.7k times · Source

I have a fairly simple app built with pyqt4. I wanted to debug one of the functions connected to one of the buttons in my app. However, when I do the following

python -m pdb app.pyw
> break app.pyw:55  # This is where the signal handling function starts.

things don't quite work like I'd hope. Instead of breaking in the function where I've set the breakpoint and letting me step through it, the debugger enters an infinite loop printing out QCoreApplication::exec: The event loop is already running and I am unable to input anything. Is there a better way to do this?

Answer

quark picture quark · Nov 17, 2009

You need to call QtCore.pyqtRemoveInputHook. I wrap it in my own version of set_trace:

def debug_trace():
  '''Set a tracepoint in the Python debugger that works with Qt'''
  from PyQt4.QtCore import pyqtRemoveInputHook

  # Or for Qt5
  #from PyQt5.QtCore import pyqtRemoveInputHook

  from pdb import set_trace
  pyqtRemoveInputHook()
  set_trace()

And when you are done debugging, you can call QtCore.pyqtRestoreInputHook(), probably best when you are still in pdb, and then after you hit enter, and the console spam is happening, keep hitting 'c' (for continue) until the app resumes properly. (I had to hit 'c' several times for some reason, it kept going back into pdb, but after hitting it a few times it resumed normally)

For further info Google "pyqtRemoveInputHook pdb". (Really obvious isn't it? ;P)