pythonpyqt5qdialog

Closing a QDialog window that has been opened by another widget


I think I've run into a problem similar to [StackOverflow -- How to close a QDialog][1] but I'm not seeing a solution if one was given. I've mocked two scripts similar to what I am having troubles with below.

mainWidget.py

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
import shelve

from calledWidget import Ui_CalledWindow

class Ui_Form(object):    
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(640, 170)
        Form.setWindowTitle("Form")
        
        
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        
        
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setObjectName("lineEdit")
        self.verticalLayout.addWidget(self.lineEdit)
        
        
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("PushButton")
        self.verticalLayout.addWidget(self.pushButton)
        self.pushButton.clicked.connect(self.passString)       
        
    def passString(self):
        stringToPass = self.lineEdit.text()
        
        openingDialog = QMessageBox()
        openingDialog.setWindowTitle("Warning")
        openingDialog.setText("Are sure you want to pass the string: {}?".format(stringToPass))
        openingDialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        reply = openingDialog.exec()
        
        if reply == QMessageBox.Yes:
            s = shelve.open("tempPass")
            s["stringOut"] = stringToPass
            s.close()
            
            self.newWindow = QtWidgets.QDialog()
            self.ui = Ui_CalledWindow()
            self.ui.setupUi(self.newWindow)
            self.newWindow.show()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

calledWidget.py

from PyQt5 import QtCore, QtGui, QtWidgets
import shelve

class Ui_CalledWindow(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(612, 178)
        Dialog.setWindowTitle("Dialog")
        
        
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        
        
        s = shelve.open("tempPass")
        stringIn = s["stringOut"]
        
        self.listView = QtWidgets.QListWidget(Dialog)
        self.listView.setObjectName("listView")
        self.verticalLayout.addWidget(self.listView)
        self.listView.addItem(stringIn)
        
        
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        
        
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.verticalLayout.addWidget(self.buttonBox)
        
        
        self.buttonBox.accepted.connect(self.closePlz)
        
    def closePlz(self):
        print("Code to close current window goes here.")
        self.close()

In short, mainWidget.py takes in a string and passes it to calledWidget.py. Eventually the latter will perform operations on the passed string and give the user the option to keep the changes or modify them. For now it simply displays the string in a list and has an 'Ok' button which I would like to have close the QDialog window when clicked. I've tried using the .close() method, sys.exit(), and even a signal to the .closeEvent() to try to find a work around but nothing has worked yet. Any help is appreciated.

Edit: I get that Designer has its own set of rules for how to go about editing it but I am using the results as an example to quickly illustrate the point. If I've made a mistake as a result of using Designer please let me know so I can learn for the future but my main question is how does closing a QDialog box work when it is called from another widget. [1]: How to close a QDialog


Solution

  • Current work around: in method passString of script mainWidget.py

    def passString(self):
            stringToPass = self.lineEdit.text()
            
            openingDialog = QMessageBox()
            openingDialog.setWindowTitle("Warning")
            openingDialog.setText("Are sure you want to pass the string: {}?".format(stringToPass))
            openingDialog.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            reply = openingDialog.exec()
            
            if reply == QMessageBox.Yes:
                s = shelve.open("tempPass")
                s["stringOut"] = stringToPass
                s.close()
                
                self.newWindow = QtWidgets.QDialog()
                self.ui = Ui_CalledWindow()
                self.ui.setupUi(self.newWindow)
                self.newWindow.show()
            
            # The following connects to the Ok button in the new window and closes it    
            self.ui.buttonBox.clicked.connect(self.newWindow.close)