Luk*_*uke 34 python pyqt pyqt4
我喜欢python和Qt,但对我来说很明显,Qt并不是用python设计的.有许多方法可以使PyQt/PySide应用程序崩溃,其中许多方法都非常难以调试,即使使用适当的工具也是如此.
我想知道:使用PyQt和PySide时,避免崩溃和锁定的好方法是什么?这些可以是从一般编程技巧和支持模块到高度特定的变通方法和要避免的错误.
Luk*_*uke 51
请注意Qt自动删除对象的情况.如果没有通知python包装器已删除C++对象,那么访问它将导致崩溃.由于PyQt和PySide在跟踪Qt对象方面存在困难,因此可能会以多种方式发生这种情况.
从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)