I have a parent class which handles opening projects. Projects can be opened from a child window which calls the parent function to handle opening the project. However, when a file-dialog is cancelled from the child window, the entire application exits.
from PyQt5.QtCore import Qt, QDateTime
from PyQt5.QtWidgets import *
from PyQt5 import QtGui
class ParentWindow(QDialog):
def __init__(self):
super(ParentWindow, self).__init__()
self.cw = None
self.setFixedSize(300, 100)
self.button = QPushButton('Open')
self.button.clicked.connect(self.open)
layout = QHBoxLayout()
layout.addWidget(self.button)
self.setLayout(layout)
self.show()
def open(self):
fileDialog = QFileDialog(self, 'Projects')
fileDialog.setFileMode(QFileDialog.DirectoryOnly)
if fileDialog.exec():
self.hide()
name = fileDialog.selectedFiles()[0]
if self.cw:
self.cw.close()
self.cw = ChildWindow(self, name)
class ChildWindow(QDialog):
def __init__(self, parent, name):
super(ChildWindow, self).__init__(parent)
self.setFixedSize(500, 100)
self.setWindowTitle(name)
self.openButton = QPushButton('Open')
self.openButton.clicked.connect(self.parent().open)
layout = QHBoxLayout()
layout.addWidget(self.openButton)
self.setLayout(layout)
self.show()
I can't figure out why the program won't return to the child window when cancel is pressed in the file-dialg. Is there a way to keep the parent responsible for opening projects and fix this issue?