为什么 PyQt 有时会在退出时崩溃?

Why does PyQt sometimes crash on exit?(为什么 PyQt 有时会在退出时崩溃?)
本文介绍了为什么 PyQt 有时会在退出时崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

下面给定的代码显示了一个 QMainWindow 和 4 个 QGraphicsView 以在其中使用鼠标进行绘制.它按预期工作,但在关闭它时,控制台中会出现以下错误消息:

The given code below displays a QMainWindow with 4 QGraphicsView to draw with the mouse in it. It works as intended, but when closing it the following error message appears in the console:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

代码有什么问题?

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QGraphicsPathItem
from PyQt5.QtGui import QPainterPath, QPen
from PyQt5.QtCore import Qt
from PyQt5.uic import loadUi

# Based on code from https://stackoverflow.com/a/44248794/7481773

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        loadUi("mainwindow.ui", self)

        self.verticalLayout_top_left.addWidget(GraphicsView())
        self.verticalLayout_top_right.addWidget(GraphicsView())
        self.verticalLayout_bottom_left.addWidget(GraphicsView())
        self.verticalLayout_bottom_right.addWidget(GraphicsView())


class GraphicsView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.start = None
        self.end = None

        self.setScene(QGraphicsScene())
        self.path = QPainterPath()
        self.item = GraphicsPathItem()
        self.scene().addItem(self.item)

        self.contents_rect = self.contentsRect()
        self.setSceneRect(0, 0, self.contents_rect.width(), self.contents_rect.height())
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

    def mousePressEvent(self, event):
        self.start = self.mapToScene(event.pos())
        self.path.moveTo(self.start)

    def mouseMoveEvent(self, event):
        self.end = self.mapToScene(event.pos())
        self.path.lineTo(self.end)
        self.start = self.end
        self.item.setPath(self.path)


class GraphicsPathItem(QGraphicsPathItem):
    def __init__(self):
        super().__init__()
        pen = QPen()
        pen.setColor(Qt.black)
        pen.setWidth(5)
        self.setPen(pen)


def main():
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    app.exec_()


if __name__ == "__main__":
    main()

<小时>

主窗口.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="1" column="0">
     <widget class="QPushButton" name="pushButton_left">
      <property name="text">
       <string>PushButton</string>
      </property>
     </widget>
    </item>
    <item row="0" column="0">
     <layout class="QVBoxLayout" name="verticalLayout_top_left"/>
    </item>
    <item row="0" column="1">
     <layout class="QVBoxLayout" name="verticalLayout_top_right"/>
    </item>
    <item row="1" column="1">
     <widget class="QPushButton" name="pushButton_right">
      <property name="text">
       <string>PushButton</string>
      </property>
     </widget>
    </item>
    <item row="2" column="0">
     <layout class="QVBoxLayout" name="verticalLayout_bottom_left"/>
    </item>
    <item row="2" column="1">
     <layout class="QVBoxLayout" name="verticalLayout_bottom_right"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>24</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

推荐答案

问题是因为即使你完成运行 app.exec_() 应用程序仍然释放需要访问 QApplication 实例的资源,但在你的情况下app"在这样做之前通过使Qt Access的内部函数不保留内存而被删除.

The problem is caused because even when you finish running app.exec_() the application still releases resources that need access to the instance of QApplication but as in your case "app" is removed before doing so by making the internal functions of Qt Access unreserved memory.

考虑到上述情况,一个可能的解决方案是通过制作一个全局变量来扩展app"的范围,使其在执行主函数后不会被删除.

Considering the above, a possible solution is to extend the scope of "app" so that it is not removed after executing main function by making a global variable.

# ...
app = None


def main():
    global app
    app = QApplication(sys.argv)
    # ...

这篇关于为什么 PyQt 有时会在退出时崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Why does Python#39;s IDLE crash when I type a parenthesis on Mac?(为什么我在 Mac 上键入括号时 Python 的 IDLE 会崩溃?)
Tkinter crashes Jupyter kernel?(Tkinter 崩溃 Jupyter 内核?)
IDLE crash when opening on Mac OS X(在 Mac OS X 上打开时 IDLE 崩溃)
GVIM crashes when running python(运行 python 时 GVIM 崩溃)
PySide: Segfault(?) when using QItemSelectionModel with QListView(PySide:将 QItemSelectionModel 与 QListView 一起使用时的 Segfault(?))
Debugging Python Fatal Error: GC Object already Tracked(调试 Python 致命错误:已跟踪 GC 对象)