Important: Please read the Qt Code of Conduct -

Application segfaults on close

  • Hi, I wrote a small test of "layers," for a PyQt app that I'm working on. When I was using PyQt4, there were no segfualts, but as soon as I switched it to PyQt5, it segfaults on close.

    It seems to be something with setting the scene for the QGraphicsView. If there is no scene, there is no segfault, yet if there is it will upon closing the app. Line 30 of the cod ebelow notes this.


    A simple example to implement a layer feature, it tests:

    -- Layers

    -- .gif load and aniamiton

    -- Zooming

    All if this should be done using the QGraphicsView Framework

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *

    TODO, this is now segfaulting because of update to PyQt5, fix it

    class LayerWidget(QWidget):

    def __init__(self, parent=None):
        super(LayerWidget, self).__init__(parent)
        # Some Varibales
        self.scale = 2
        self.viewSize = QSize()         # Size we want for the view at all times
        # Get the scene
        self.scene = LayerScene(self)
        # Set the view
        self.view = QGraphicsView(self)
        self.view.setScene(self.scene)              # Doesn't segfault if a scene is not set
        self.view.scale(self.scale, self.scale)
        self.viewSize = self.scene.sceneRect().toRect().size() * self.scale
        # Other
        self.setWindowTitle('Simple Layers Test (w/ Animation)')
    def resizeEvent(self, event):
        # Try to keep the view in the center of the window at all times
        widgetSize = self.size()
        viewSize = self.viewSize
        # Check the sizes
        widgetWidthSmaller = widgetSize.width() < viewSize.width()
        widgetHeightSmaller = widgetSize.height() < viewSize.height()
        if widgetWidthSmaller or widgetHeightSmaller:
            # too small, resize the view
            # Make the new sized view
            newSize = QSize()
            newSize.setWidth(widgetSize.width() if widgetWidthSmaller else viewSize.width())
            newSize.setHeight(widgetSize.height() if widgetHeightSmaller else viewSize.height())
            newLoc = QPoint()
            newLoc.setX(0 if widgetWidthSmaller else self.view.pos().x())
            newLoc.setY(0 if widgetHeightSmaller else self.view.pos().y())
            # Change the minimum size and resize the view
            # No scrolly-bars
            # Keep it centered
            viewRect = self.view.rect()

    class LayerScene(QGraphicsScene):
    # Represents a "frame," with multiple layers

    def __init__(self, parent=None):
        super(LayerScene, self).__init__(parent)
        self.layer = []
        # Hard code in some images
        self.layer[1].moveBy(50, 50)

    class AnimatedGIF(QGraphicsPixmapItem):
    # A small test of an animated gif playing in a graphics scene

    def __init__(self, filename, parent=None):
        super(AnimatedGIF, self).__init__(parent)
        self.gif = QMovie(filename)
    def nextFrame(self, n):
    def start(self):

    def main():
    app = QApplication(sys.argv)
    lw = LayerWidget()



    Oddly enough, I had another test application that used QGraphicsView and QGraphicsScene in a similar manner. It used to segfault all the time in PyQt4, but it doesn't in PyQt5.

    Any help here would be appreciated.

Log in to reply