[PyQt] Conflict between setuptools & requirements in official PyQt5 docs

Phil Thompson phil at riverbankcomputing.com
Wed Feb 10 17:24:47 GMT 2016


On 10 Feb 2016, at 4:49 pm, Jones, Bryan <bjones at ece.msstate.edu> wrote:
> 
> Phil, Kovid, and all,
> 
> Thanks for your thoughts on this. The difficulty of correctly reconciling Qt's memory management scheme (you must insure all objects are destroyed in the correct order) with Python's (the interpreter chooses an order which you have little control over) has puzzled me for a while. The following code crashes several ways, even with Kovid's suggestion. I'm running under Qt 5.5.1, SIP 4.17, PtQt 5.5.1, Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)], OS nt, platform Windows 7.
> 
> Bryan
> 
> import sys
> import os
> import platform
> import gc
> import sip
> from PyQt5.QtWidgets import QApplication, QWidget
> from PyQt5.QtCore import QT_VERSION_STR
> from PyQt5.Qt import PYQT_VERSION_STR
> 
> app = None
> 
> def printDestroyed(qObject):
>     def destroyed():
>         print('destroyed {}'.format(qObject))
>     qObject.destroyed.connect(destroyed)
> 
> def quickCrash():
>     print('quickCrash')
>     QApplication(sys.argv)
>     QWidget()

That will crash because you aren't keeping references to the objects.

> def slowCrash():
>     global app
>     print('slowCrash')
>     app = QApplication(sys.argv)
>     printDestroyed(app)
> 
>     w = QWidget()
>     printDestroyed(w)
>     w.show()
> 
>     sip.delete(app)
>     sip.delete(w)

That negates the effects of the global app and it wouldn't surprise me that Qt doesn't like widgets being destroyed after the QApplication.

> def qtMain():
>     global app
>     app = QApplication(sys.argv)
>     printDestroyed(app)
> 
>     w = QWidget()
>     printDestroyed(w)
>     w.show()
> 
> def gcCrash():
>     print('gcCrash')
>     qtMain()
>     gc.collect()

I think that has something to do with the destroyed() slot being in a nested function. Also the destroyed signal can be tricky to deal with because it is emitted during the destruction of the QObject. I'm not saying there isn't a bug here, but I'm not sure it's a symptom of the crash on exit issue.

Phil


More information about the PyQt mailing list