0

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.

It's not a complex Qt app and I'm really struggling trying to understand what happens.

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. 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.

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
sevenup
  • 170
  • 8
  • Isn't there a stack trace when python crashes? – sashoalm Jan 21 '14 at 14:54
  • Well actually it works fine on Linux, and when I run it under ipython on Windows there's no traceback but the QDialogs stay open and I have to close them manually. I can't get any traceback on Windows. – sevenup Jan 21 '14 at 15:02
  • So the word 'crash' you use is not like [that kind of crash](http://img205.imageshack.us/img205/9509/winmysqladminxamppdv3.jpg)? – sashoalm Jan 21 '14 at 15:16
  • Well yes it is, when I try to debug the python process in VS2012 this is the error I get: Unhandled exception at 0x66CDB906 (QtCore4.dll) in python.exe: 0xC0000005: Access violation reading location 0x00000000. – sevenup Jan 21 '14 at 15:19
  • For *ipython* you mean the `qtconsole`? Maybe in that case there's some interaction between ipython's `QApplication` and your code (however it all depends on how are you running the code etc.) – Bakuriu Jan 21 '14 at 15:56

1 Answers1

0

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:

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.

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