在pyqt4中,在this answer中給出的退出時崩潰的解決方法工作。在pyQt5中退出時出現分段錯誤,但不在pyQt4中
但是不能使用pyqt5,其中經常(超過一半的時間)出現分段錯誤。
只有進口線改變
#!/usr/bin/python
import sys
#toolkit = "Qt4"
toolkit = "Qt5"
if toolkit == "Qt4":
# Qt4 (no crash)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
elif toolkit == "Qt5":
# Qt5 (crash)
from PyQt5.QtWidgets import (
QApplication, QGraphicsScene, QGraphicsView
)
app = QApplication(sys.argv)
grview = QGraphicsView()
# no crash
scene = QGraphicsScene(parent=grview)
grview.setScene(scene)
grview.show()
sys.exit(app.exec_())
這裏是回溯,from the core dump(作爲一個側面說明,GDB裏,有沒有崩潰)
[New LWP 4684]
[New LWP 4683]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `python grview_qt5.py'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00007f5233393e69 in ??()
(gdb) bt
#0 0x00007f5233393e69 in ()
#1 0x00007f522fee6f20 in ()
#2 0x0000000000e0d340 in ()
#3 0x00007f522fee6f20 in ()
#4 0x0000000000e0d5f0 in ()
#5 0x00007f523e0bc000 in _rtld_local() at /lib64/ld-linux-x86-64.so.2
#6 0x00007f523ae7285f in QThreadPrivate::start(void*) (arg=0xe0d340) at thread/qthread_unix.cpp:337
#7 0x00007f523d8780a4 in start_thread (arg=0x7f522fee7700) at pthread_create.c:309
#8 0x00007f523d5adcbd in clone() at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
(gdb) info threads
Id Target Id Frame
2 Thread 0x7f523e06f700 (LWP 4683) 0x00007f523deb21c7 in munmap() at ../sysdeps/unix/syscall-template.S:81
* 1 Thread 0x7f522fee7700 (LWP 4684) 0x00007f5233393e69 in ??()
爲什麼PyQt4的和pyqt5之間的區別?
這肯定與Object Destruction on Exit有關,但是怎麼樣?
什麼是避免這種崩潰的正確方法?
python3在不同的時間刪除對象,然後python2。嘗試在sys.exit之前使用顯式del對象:del grview; del scene; del app ;. –
用'QtCore.QTimer.singleShot(1000,app.quit)'替換所有圖形內容,這樣只創建一個'QApplication'。如果仍然崩潰,請使用'QCoreApplication'來代替。這可能有助於縮小問題的根源。 – ekhumoro
@ekhumoro將所有圖形的東西替換爲'QTimer.singleShot(1000,app.quit)',沒有崩潰。 – ederag