简体   繁体   English

在打开qdialog的情况下关闭qmainwindow时Python崩溃

[英]Python crash when closing qmainwindow with qdialog open

I have a QMainWindow that launches a QDialog everytime I click on a button and I can't figure out why the python binary crashes when I close the QMainWindow while one or more dialogs are open. 我有一个QMainWindow,每次单击按钮时都会启动QDialog,我不知道为什么在打开一个或多个对话框时关闭QMainWindow时python二进制文件崩溃的原因。

It's not a complex Qt app and I'm really struggling trying to understand what happens. 它不是一个复杂的Qt应用程序,我真的很努力尝试了解发生了什么。

Here's the code: 这是代码:

# dependency modules
from PyQt4 import QtGui
import sys

# custom modules
from ui import SingleOrderUI, DashBoardUI

class SingleOrder(QtGui.QDialog, SingleOrderUI.Ui_SingleOrder):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)

class DashBoard(QtGui.QMainWindow, DashBoardUI.Ui_DashBoard):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        super(DashBoard, self).__init__()

        # setup UI
        self.setupUi(self)

        self.newOrderBtn.clicked.connect(self.newOrder)


    def newOrder(self):
        print 'New order clicked'
        so = SingleOrder(self)
        so.show()      

app = QtGui.QApplication(sys.argv)
window = DashBoard()
window.show()
sys.exit(app.exec_())

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: When launched using ipython, the dialogs are still showing after I close the QMainWindow, so that's maybe where the issue comes from. 编辑:当使用ipython启动时,关闭QMainWindow之后,对话框仍然显示,所以这可能是问题所在。 I give the QMainWindow as a parent argument to the QDialog, I thought that was enough to have them killed when the QMainWindow is closed. 我将QMainWindow作为QDialog的父参数,我认为当QMainWindow关闭时足以杀死它们。

Okay, I've found a workaround for that but I'm not sure if it's the right way to do it. 好的,我已经找到了解决方法,但是我不确定这是否是正确的方法。

On my DashBoard init method, I've added a python list that will store all the opened Dialogs: 在DashBoard的init方法上,我添加了一个python列表,该列表将存储所有打开的对话框:

def __init__(self):
    QtGui.QMainWindow.__init__(self)
    super(DashBoard, self).__init__()

    # setup UI
    self.setupUi(self)
    self.newOrderBtn.clicked.connect(self.newOrder)

    self.soTab = []

Then, in the same class, I defined a method to handle the closeEvent and close all the dialogs. 然后,在同一个类中,我定义了一个方法来处理closeEvent并关闭所有对话框。

def closeEvent(self, event):
    for so in self.soTab:
        if so:
            so.close()
    event.accept()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM