50

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?

3
  • Just to know, what's on line 55? Is where you register the signal or is it the function that handles it? Nov 15, 2009 at 0:36
  • In this case, that is the function that handles the signal.
    – Paul Wicks
    Nov 15, 2009 at 8:49
  • 1
    I think this is not a fully answer, but you could try ERIC as a GUI-Debugger for Python and PyQt: eric-ide.python-projects.org
    – Berschi
    Nov 15, 2009 at 11:19

3 Answers 3

95

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)

1
  • 5
    Useful answer, just for reference I found that this worked with PyQt4, but not with PySide. Nov 7, 2011 at 13:30
6

I had to use a "next" command at the trace point to get outside of that function first. For that I made a modification of the code from mgrandi:

def pyqt_set_trace():
    '''Set a tracepoint in the Python debugger that works with Qt'''
    from PyQt4.QtCore import pyqtRemoveInputHook
    import pdb
    import sys
    pyqtRemoveInputHook()
    # set up the debugger
    debugger = pdb.Pdb()
    debugger.reset()
    # custom next to get outside of function scope
    debugger.do_next(None) # run the next command
    users_frame = sys._getframe().f_back # frame where the user invoked `pyqt_set_trace()`
    debugger.interaction(users_frame, None)

This worked for me. I found the solution from here : Python (pdb) - Queueing up commands to execute

1
  • pdb.Pdb() now causes a segfault with pyqt5
    – nmz787
    Mar 31 at 16:46
0

In my tests, jamk's solution works, while the previous one, although simpler, does not.

In some situations, for reasons that are unclear to me, I've been able to debug Qt without doing any of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.