Segmentation fault when exiting PyQT app
<p>First, I have tried my best to find a solution to this problem here and other places and I am have a general idea of what the problem is, but it is not clear to me how to solve it. </p>

<p>The basic problem is that I am getting a segmentation fault when I close my app by pressing the standard "x" button. </p>

<p>The most important details (I think) are that I am using MacOS Sierra, python 3.5.2, and pyqt5. </p>

<p>The app I am building is very loosely based on another project (<a href="https://github.com/Dioptas/Dioptas" rel="nofollow noreferrer">Dioptas</a>), which is a relatively mature project. I am more or less getting started.</p>

<p>When I close the window, the terminal prints out as instructed in MainController.close_event():</p>

<pre><code>&gt; here
&gt; closed
&gt; accepted
&gt; Segmentation fault: 11
</code></pre>

<p>I have tried many of the suggestions online and I am fairly sure that this is due to python not closing all of the windows (perhaps due to the order in which they are being closed - QApplication.CloseAllWindows() says they are closed in a random order, for one thing).  If anyone has a suggestion or solution I would really appreciate it.</p>

<p>The following is my code:</p>

<pre><code>import sys
import pyqtgraph as pg
import numpy as np
from PIL import Image
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class MainController(QWidget):
    def __init__(self):
        super().__init__
        self.start()
        self.create_signals()

    def start(self):
        self.widget = MainWidget()
        self.widget.show()

    def create_signals(self):
        self.widget.closeEvent = self.close_event

    def close_event(self, ev):
        print("here")
        QApplication.closeAllWindows()
        print("closed")
        ev.accept()

class MainWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super(MainWidget, self).__init__(*args, **kwargs)
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(2, 2, 2, 2)
        self.layout.setSpacing(6)

        self.stepFilterDisplayWidget = StepFilterDisplayWidget()
        self.stepFilterControlWidget = StepFilterControlWidget()
        self.layout.addWidget(self.stepFilterDisplayWidget)
        self.layout.addWidget(self.stepFilterControlWidget)
        self.setLayout(self.layout)
        self.setGeometry(100,100,1000,700)

class StepFilterDisplayWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super(StepFilterDisplayWidget,self).__init__(*args,**kwargs)

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        self.plot = pg.ImageView()
        self.layout.addWidget(self.plot)

        self.button = QPushButton("Plot", self)
        self.button.clicked.connect(self.showImage)

        self.layout.addWidget(self.button)

    def showImage(self):
        im = Image.open('S_15a_crop.tif')
        self.data = np.array(im)
        self.plot.setImage(self.data)


class StepFilterControlWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super(StepFilterControlWidget, self).__init__(*args, **kwargs)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    controller = MainController()
    app.exec_()
    del app
</code></pre>