PyQt4 application on Windows is crashing on exit

老子叫甜甜 提交于 2019-12-24 02:23:43

问题


I am writting a desktop application with PyQt4 and all of the sudden it started to crash on exit.

I reviewed all of my code to make sure I wasn't doing anything funny to make it crash and I don't think there's anything wrong with the code.

I have seen some complaints about this before but it was related to a previous version and people advised to upgrade PyQt4 to the latest version and so I did, but that didn't help with the crashing problem.

So I ask, is there anything that can lead to this behavior with PyQt4, do I need to do some kind on termination procedure to cleanup Qt or anything else I am missing?


回答1:


I had the same problem with a simple hello world application (QDialog with 20 labels). Weirdly the problem disappears with 10 labels.

I solved by forcing exit as follows:

def closeEvent(self, event):
    exit()

This happens on Windows with PyQt v4.10.3 for Python v2.7 (x32) on VirtualBox.




回答2:


A debugger will only tell us what we already know: The application crashes on exit.

You probably need to set an active window which, when closed, will result in deterministic garbage collection and a clean application exit. There are more proper ways to do this, but the simpl;e example below should require minimum code changes, and is based on a dialog application that I wrote and works fine.

#The application
app = QtGui.QApplication(sys.argv)

#The main window
MainWindow = QtGui.QMainWindow()
app.setActiveWindow(MainWindow) #<---- This is what's probably missing

#the ui
ui = Ui_MainWindow()
ui.setupUi(MainWindow)

#start the application's exec loop, return the exit code to the OS
exit(app.exec_())


来源:https://stackoverflow.com/questions/5506781/pyqt4-application-on-windows-is-crashing-on-exit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!