How to close a QDialog

C Snyder picture C Snyder · Jun 30, 2015 · Viewed 19.5k times · Source

I've been trying to close a QDialog window that is branching off of my main window. The following have not worked for me so far:

self.close()
QDialog.close()

I tried other commands such as exit and exec_() with no luck. The most common error I get is

[className] object has no attribute 'close'

# Creating our window
class Ui_MainWindow(object):

    # Sets up GUI
    def setupUi(self, MainWindow):

        [GUI CODE]      

    # Sets text for parts of GUI
    def retranslateUi(self, MainWindow):

        [MORE GUI CODE]

    # Function handling screencap on click and metadata for filenames
    def cap_on_Click(arg1,arg2):

        popup = QDialog()
        popup_ui = Ui_Dialog()
        popup_ui.setupUi(popup)
        popup.show()
        sys.exit(popup.exec_())

The above is my main window

class Ui_Dialog(object):

    def setupUi(self, Dialog):

        [GUI CODE]

    def retranslateUi(self, Dialog):

        [MORE GUI CODE]

    def button_click(self, arg1):

        self.close()

The second block is the dialog window code. How do I close this dialog window?

Answer

ForceBru picture ForceBru · Jun 30, 2015

First of all, sorry for the links related to C++, but Python has the same concept.

You can try using the reject or accept or done function to close the dialog box. By doing this, you're setting the return value appropriately (Rejected, Accepted or the one you specify as the argument).

All in all, you should try calling YourDialog.done(n) to close the dialog and return n and YourDialog.accept() or YourDialog.reject() when you want it accepted/rejected.