Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a QDialog

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?

like image 534
C Snyder Avatar asked Jun 30 '15 19:06

C Snyder


2 Answers

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.

like image 66
ForceBru Avatar answered Oct 25 '22 07:10

ForceBru


I guess the problem is that Ui_Dialog does not inherit QDialog, so reject(), accept() and done() is not defined. I think

class Ui_Dialog(object):

should be changed to

class Ui_Dialog(QDialog):

But I can not test it because minimal working example is not provided.

like image 27
cges30901 Avatar answered Oct 25 '22 09:10

cges30901