Results 1 to 7 of 7

Thread: closeEvent question

  1. #1
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default closeEvent question

    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?

    Qt Code:
    1. class Tool1(QtGui.QDialog, ui_tool_1_1.Ui_tool1_1_Dialog):
    2. """Tool1 dialog"""
    3.  
    4. # class Initialization
    5. def __init__(self, parent=None):
    6.  
    7. # python super so script does not need to call the methods by name.
    8. super(Tool1, self).__init__(parent)
    9. self.setupUi(self)
    10.  
    11. # Push button to open a new gui.
    12. self.add_evt_pushButton.clicked.connect(self.addEvt)
    13.  
    14. def addEvt(self):
    15. test = AppField(parent=self)
    16. if test.exec_():
    17. print"hello"
    18.  
    19.  
    20. class AppField(QtGui.QDialog, ui_app_field.Ui_AppFieldDialog):
    21. """
    22. Tool 2.
    23. """
    24. # class Initialization
    25. def __init__(self, parent=None):
    26. super(AppField, self).__init__(parent)
    27. self.setupUi(self)
    28.  
    29. # connection to okay button.
    30. self.ok_pushButton.clicked.connect(self.okay_button)
    31.  
    32. def okay_button(self):
    33. """
    34. handles the ok push button.
    35. """
    36. self.close()
    37.  
    38. def closeEvent(self, event):
    39. if QtGui.QMessageBox.question(self, "warning", "do you want to close",
    40. QtGui.QMessageBox.Yes|QtGui.QMessageBox.No) == QtGui.QMessageBox.No:
    41. event.ignore()
    42. else:
    43. event.accept()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: closeEvent question

    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,
    _

  3. #3
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default Re: closeEvent question

    I am not sure that is right. Even if you comment out the closeEvent method the same results happen.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: closeEvent question

    QDialog::closeEvent() calls reject(), which exits exec() with QDialog::Rejected:
    http://code.woboq.org/qt5/qtbase/src...g.cpp.html#690

    Cheers,
    _

  5. #5
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default Re: closeEvent question

    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().

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: closeEvent question

    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,
    _

  7. #7
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default Re: closeEvent question

    Thank you anda_skoa. Now that makes sense. Thank you for your help.

Similar Threads

  1. CloseEvent error
    By hollowhead in forum Newbie
    Replies: 2
    Last Post: 10th March 2012, 11:21
  2. CloseEvent of QDockWidget
    By mstegehu in forum Qt Programming
    Replies: 5
    Last Post: 16th March 2010, 13:10
  3. QDialog and closeEvent
    By ricardo in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2009, 02:07
  4. simple Question about QDialog::closeEvent(...)
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2008, 18:09
  5. closeEvent
    By jochen_r in forum Newbie
    Replies: 7
    Last Post: 16th January 2006, 12:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.