PyQt5 GUI crashes when QDialog from different module is closed

19 Views Asked by At

I made a helper window with PyQt5 to load some data in it's own module and when used in a simple scripts it works. Here is the important parts of the code:

def read_dialog():
    app = QApplication(sys.argv)
    window = HelperWindow()
    status = window.exec()
    if status == 0:
        data = window.get_data()
    elif status == 1:
        print('Warning: Dialog rejected. Returning empty list.')
        data = []

    return data


class HelperWindow(QDialog):
    def __init__(self):
        # code to set up the UI

    def load_button_clicked(self):
        self.done(0)
    
    def get_data(self):
        data = []
        # do some stuff
        return data

My other script is importing these functions and looks like this:

from utils import read_dialog

class mainWindow(QMainWindow):
    def __init__(self):
        # code to set up a layout
        widget = QWidget()
        widget.setLayout(self.layout)
        self.setCentralWidget(widget)

        self.show()

    def load_data(self):
        data = read_dialog()
        if data != []:
            self.do_something_with_data(data)

    def do_something_with_data(self, data):
        pass

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = mainWindow()
    app.exec()

The code crashes without any errormessage if I hit the load button, even in debug mode (PyCharm CE 2023). However if I set a breakpoint at the line return data in the function read_dialog, and then resume to the code it does not crash.

Does it have to do with the window.get_data() being called and then trying returning the data before it has finished? I read something that in PyQt having multiple threads that are not spawned by the main program can cause problems, so I am wondering if QDialog is opening a new thread here.

0

There are 0 best solutions below