PDA

View Full Version : closeEvent question



lukeQt
29th July 2015, 17:22
Hi Everyone,

I have a question with the closeEvent method. If I use self.accept() in the close event I get what I am expecting. If I use event.accept() the window did not execute. Why is event.accept() not working? See below for the example.

After the second window closes I was expecting the print statement to print. Am I not understanding how exec_() works?



class Tool1(QtGui.QDialog, ui_tool_1_1.Ui_tool1_1_Dialog):
"""Tool1 dialog"""

# class Initialization
def __init__(self, parent=None):

# python super so script does not need to call the methods by name.
super(Tool1, self).__init__(parent)
self.setupUi(self)

# Push button to open a new gui.
self.add_evt_pushButton.clicked.connect(self.addEv t)

def addEvt(self):
test = AppField(parent=self)
if test.exec_():
print"hello"


class AppField(QtGui.QDialog, ui_app_field.Ui_AppFieldDialog):
"""
Tool 2.
"""
# class Initialization
def __init__(self, parent=None):
super(AppField, self).__init__(parent)
self.setupUi(self)

# connection to okay button.
self.ok_pushButton.clicked.connect(self.okay_butto n)

def okay_button(self):
"""
handles the ok push button.
"""
self.close()

def closeEvent(self, event):
if QtGui.QMessageBox.question(self, "warning", "do you want to close",
QtGui.QMessageBox.Yes|QtGui.QMessageBox.No) == QtGui.QMessageBox.No:
event.ignore()
else:
event.accept()

anda_skoa
29th July 2015, 19:10
You override the default behavior of QDialog::closeEvent() which most likely calls QDialog::reject().
Either call that youself or call the base class implementation if you want to accept the event and close the dialog.

Cheers,
_

lukeQt
29th July 2015, 19:19
I am not sure that is right. Even if you comment out the closeEvent method the same results happen.

anda_skoa
30th July 2015, 08:33
QDialog::closeEvent() calls reject(), which exits exec() with QDialog::Rejected:
http://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qdialog.cpp.html#690

Cheers,
_

lukeQt
30th July 2015, 19:44
Thank you Anda_skoa!!! My understanding of self.close() got the better of me. self.close(), closes the dialog if it is a parent. If it is a child, then you would use self.accept().

anda_skoa
30th July 2015, 21:20
No.

close() closes the window. On a dialog this results in reject() being called.
Reject results in exec() returning QDialog::rejected.

accept() is usually connected to the "OK" button or similar. Calling it results in exec() returning QDialog::Accepted.

Both reject() and accept() can be overwritten to do stuff.
E.g. accept() is often overwritten to do a final check on input data. The dialog stays open ss long as the overwritten method does not call the base class method.
E.g reject() is sometimes overwritten to reset values.

Cheers,
_

lukeQt
31st July 2015, 20:18
Thank you anda_skoa. Now that makes sense. Thank you for your help.