Segmentation fault when exiting PyQT app

Question:

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.

The basic problem is that I am getting a segmentation fault when I close my app by pressing the standard “x” button.

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

The app I am building is very loosely based on another project (Dioptas), which is a relatively mature project. I am more or less getting started.

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

> here
> closed
> accepted
> Segmentation fault: 11

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.

The following is my 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
Asked By: user7229080

||

Answers:

The problem is about pyqtgraph (which uses PyQt4) and PyQt5 imports. pyqtgraph trying to use something belongs to PyQt4 which was overriden by PyQt5 import. This triggers the segmentation fault.

Answered By: obayhan

I was having the same problem – with pyqt5 and python3.10
My app has 3 pyqtgraph widgets:

  • 1 pg.ImageView() created directly in the main application which is a QtWidgets.QMainWindow()
  • 2 pg.PlotWidget() + pg.opengl.GLViewWidget() loaded into the mainwidow application from another class QueryBuilder() which is QWidget

To quit the app I was using a call to qApp.quit() action. When only the pg.ImageView() was loaded – I had no core dump issue. The segfault issue came just after adding the QueryBuilder() code.
Adding, into the quit method, a call to close the QueryBuilder() widget fixed the issue


from querybuilder import QueryBuilder   # widgets with pyqtgraph

class MapDisplay():
    ...
    self.querybuilder = QueryBuilder()
    ...
    
    def quit(self):
        self.querybuilder.close()
        qApp.quit()
Answered By: epifanio
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.