python - 在 PyQt 中避免崩溃/挂起的良好做法是什么?

标签 python pyqt pyqt4

我喜欢 python 和 Qt,但很明显,Qt 在设计时并没有考虑到 python。有许多方法可以使 PyQt/PySide 应用程序崩溃,其中许多方法都非常难以调试,即使使用适当的工具也是如此。

我想知道:在使用 PyQt 和 PySide 时,有哪些好的做法可以避免崩溃和锁定?这些可以是任何东西,从一般的编程技巧和支持模块到高度具体的解决方法和要避免的错误。

最佳答案

一般编程实践

  • 如果您必须使用多线程代码,切勿从非 GUI 线程访问 GUI。始终通过发出信号或其他线程安全机制向 GUI 线程发送消息。
  • 小心使用模型/ View 。 TableView、TreeView 等。它们很难正确编程,任何错误都会导致无法追踪的崩溃。使用Model Test以帮助确保您的模型在内部保持一致。
  • 了解 Qt 对象管理与 Python 对象管理交互的方式以及可能出错的情况。见 http://python-camelot.s3.amazonaws.com/gpl/release/pyqt/doc/advanced/development.html
    • 没有父对象的 Qt 对象由 Python “拥有”;只有 Python 可以删除它们。
    • 具有父对象的 Qt 对象归 Qt“所有”,如果其父对象被删除,Qt 将删除它们。
    • 示例:Core dump with PyQt4
  • QObject 通常不应引用其父对象或其任何祖先(可以使用弱引用)。这最多会导致内存泄漏和偶尔的崩溃。
  • 注意 Qt 自动删除对象的情况。如果 python 包装器没有被告知 C++ 对象已被删除,那么访问它会导致崩溃。由于 PyQt 和 PySide 在跟踪 Qt 对象方面存在困难,这可能以多种不同的方式发生。

    • QScrollArea及其滚动条、QSpinBox及其QLineEdit等复合小部件(Pyside没有这个问题)
    • 删除 QObject 将 automatically delete all of its children (但是 PyQt 通常会正确处理这个问题)。
    • 从 QTreeWidget 中删除项目将导致所有关联的小部件(使用 QTreeWidget.setItemWidget 设置)被删除。

      # Example:
      from PyQt4 import QtGui, QtCore
      app = QtGui.QApplication([])
      
      # Create a QScrollArea, get a reference to one of its scroll bars.
      w = QtGui.QWidget()
      sa = QtGui.QScrollArea(w)
      sb = sa.horizontalScrollBar()
      
      # Later on, we delete the top-level widget because it was removed from the 
      # GUI and is no longer needed
      del w
      
      # At this point, Qt has automatically deleted all three widgets.
      # PyQt knows that the QScrollArea is gone and will raise an exception if
      # you try to access it:
      sa.parent()
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      RuntimeError: underlying C/C++ object has been deleted
      
      # However, PyQt does not know that the scroll bar has also been deleted.
      # Since any attempt to access the deleted object will probably cause a 
      # crash, this object is 'toxic'; remove all references to it to avoid 
      # any accidents
      sb.parent()
      # Segmentation fault (core dumped)
      

具体解决方法/错误

  • 在不先调用 prepareGeometryChange() 的情况下更改 QGraphicsItems 的边界可能会导致崩溃。
  • 在 QGraphicsItem.paint() 中引发异常可能会导致崩溃。始终在 paint() 中捕获异常并显示一条消息,而不是让异常继续未被捕获。
  • QGraphicsItems 永远不应保留对它们所在的 QGraphicsView 的引用。(weakrefs 可以)。
  • 反复使用 QTimer.singleShot 会导致死机。
  • 避免将 QGraphicsView 与 QGLWidget 一起使用。

避免退出崩溃的实践

  • 不属于 QGraphicsScene 的 QGraphicsItem 可能会导致退出时崩溃。
  • 引用其父级或任何祖先的 QObject 可能会导致退出崩溃。
  • 没有父级的 QGraphicsScene 可能会导致退出崩溃。
  • 避免退出崩溃的最简单方法是在 python 开始收集 Qt 对象之前调用 os._exit()。但是,这可能很危险,因为程序的某些部分可能依赖于正确的退出处理才能正常运行(例如,终止日志文件或正确关闭设备句柄)。至少,应该在调用 os._exit() 之前手动调用 atexit 回调。

关于python - 在 PyQt 中避免崩溃/挂起的良好做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11945183/

相关文章:

python - 使用 pandas 创建数据框而不使用 for 循环的有效方法

python - 为什么我的程序比使用 python 内置函数的程序快?

python - 带有 Svg 图像的 pyQt 悬停事件

python - 自定义锁线程python

python - 如何检查 QWidget 是否已经显示?

python - 在networkx python中,如何将一堆新的节点和边连接到图中的每个节点?

Python MySQLdb : table does not get deleted immediately

python - PyQt clicked.connect 自动连接

python - QGraphicsView如何接收鼠标移动事件?

python - 调用一个函数,然后反转同一函数的参数并再次调用