47

我有一个用 pyqt4 构建的相当简单的应用程序。我想调试连接到我的应用程序中的一个按钮的功能之一。但是,当我执行以下操作时

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

事情并不像我希望的那样工作。调试器没有中断我设置断点的函数并让我单步执行,而是进入了一个无限循环打印出来QCoreApplication::exec: The event loop is already running,我无法输入任何内容。有一个更好的方法吗?

4

3 回答 3

90

您需要调用QtCore.pyqtRemoveInputHook。我将它包装在我自己的版本中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()

当您完成调试后,您可以调用QtCore.pyqtRestoreInputHook(),最好还是在 pdb 中,然后在您按 Enter 后,控制台垃圾邮件正在发生,继续按“c”(表示继续),直到应用程序正常恢复。(由于某种原因,我不得不多次点击'c',它一直回到pdb,但在点击它几次后它恢复正常)

更多信息谷歌“pyqtRemoveInputHook pdb”。(真的很明显不是吗?;P)

于 2009-11-17T01:03:06.877 回答
5

我必须在跟踪点使用“下一个”命令才能首先脱离该功能。为此,我对 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)

这对我有用。我从这里找到了解决方案:Python (pdb) - Queuing up commands to execute

于 2014-03-18T16:32:06.973 回答
0

在我的测试中,jamk 的解决方案有效,而前一个解决方案虽然更简单,但没有。

在某些情况下,由于我不清楚的原因,我能够在不执行任何操作的情况下调试 Qt。

于 2020-03-26T20:56:30.507 回答