Skip to content Skip to sidebar Skip to footer

"python Has Stopped Working" On Pyqt5 Gui Exit

I've been pulling my hairs on this since a few hours. I have the following simple example: #!/usr/bin/env python import math from PyQt5.QtCore import (pyqtSignal, QLineF, QPointF

Solution 1:

As pointed-out by @user3419537, the catch here is to always provide a parent on widget construction.

Otherwise, deallocation goes south and you end up with a nice segfault on program termination.

The following modified code works correctly:

#!/usr/bin/env pythonimport math

from PyQt5.QtCore import (QLineF, QPointF, QRect, QRectF, QSize,
        QSizeF, Qt)
from PyQt5.QtGui import (QBrush, QColor, QFont, QIcon, QIntValidator, QPainter,
        QPainterPath, QPen, QPixmap, QPolygonF)
from PyQt5.QtWidgets import (QAction, QApplication, QButtonGroup, QComboBox,
        QFontComboBox, QGraphicsItem, QGraphicsLineItem, QGraphicsPolygonItem,
        QGraphicsScene, QGraphicsTextItem, QGraphicsView, QGridLayout,
        QHBoxLayout, QLabel, QMainWindow, QMenu, QMessageBox, QSizePolicy,
        QToolBox, QToolButton, QWidget)

classDiagramScene(QGraphicsScene):
    def__init__(self, parent=None):
        super(DiagramScene, self).__init__(parent)

classMainWindow(QMainWindow):
    def__init__(self):
        super(MainWindow, self).__init__()

        # Build Widgets, from top to bottom# Always assigning a parent to it## widget is attached to MainWindow
        self.widget = QWidget(self)
        ## view is attached to widget (main area of the MainWindow)
        self.view = QGraphicsView(self.widget)
        ## scene is attached to the view
        self.scene = DiagramScene(self.view)

        # Configure the widgets
        self.view.setScene(self.scene)

        # Configure the layout
        layout = QHBoxLayout()
        layout.addWidget(self.view)
        self.widget.setLayout(layout)
        self.setCentralWidget(self.widget)
        self.setWindowTitle("Diagramscene")

defmainFunc():
    import sys

    app = QApplication(sys.argv)

    mainWindow = MainWindow()
    mainWindow.setGeometry(100, 100, 800, 500)
    mainWindow.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    mainFunc()

Post a Comment for ""python Has Stopped Working" On Pyqt5 Gui Exit"