Reputation: 81
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?
Upvotes: 8
Views: 36518
Reputation: 251
I know it's over 5months but I choose to drop this comment here, it might be of help to others tomorrow. To be able to close or cancel an opened dialog box, using only self.close would close the entire program. use this example:
self.dlg = QDialog(self)
self.dlg.setWindowTitle("Admin")
self.dlg.setGeometry(100,100,300,200)
btnBox = QDialogButtonBox()
btnBox.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
btnBox.rejected.connect(self.close1)
def close1():
self.dlg.close()
Upvotes: 2
Reputation: 540
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.
Upvotes: 4
Reputation: 98425
Since a QDialog
is a QWidget
, and a QWidget
has the close()
method, I don't understand how it can not work. You should never invoke popup.exec_()
though, since it will happily require a lot of your code to be reentrant without you realizing it. It is unnecessary - you already have the application event loop running and on the call stack when cap_on_Click
is executing.
After popup.show()
, the dialog will be visible and usable until it gets accepted or rejected by the user. Hopefully your design for the dialog connects the button box's accepted()
and rejected()
signals to the dialog's accept()
and reject()
slots. That is the default behavior of a QDialog
template provided with Qt Creator and perhaps Qt Designer as well, but you should check it by going into the signal/slot editor mode while viewing the Ui file.
Upvotes: 3
Reputation: 44828
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.
Upvotes: 8