Results 1 to 8 of 8

Thread: Can't override the closeEvent()

  1. #1
    Join Date
    Sep 2010
    Location
    Fort Wayne, Indiana, USA
    Posts
    8
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Can't override the closeEvent()

    I have been trying to override the Windows 'X' button close to save some data with no luck. I am using PyQT 4.7.4 and Python 2.7 on Windows XP. I used Qt Designer to create the following class for the main window. This is part of the class code:

    class Ui_MainWin(object):
    def setupUi(self, MainWin):

    MainWin.setObjectName("MainWin")
    MainWin.resize(511, 362)

    In another Python module, I setup the window with the following code:

    app = QtGui.QApplication(sys.argv)
    MainWin = QtGui.QMainWindow()
    ui = Ui_MainWin()
    ui.setupUi(MainWin)

    I've searched on-line on how to override the Window's 'X' button and seen in several examples where people use the closeEvent in their window class that sets up the window:

    def closeEvent(self,event):
    print "in routine"

    When I use this code inside my class, it doesn't capture the window closing. I then thought I needed to try to connect the close event to the function (signal to slot) and use this connect statement:

    self.centralwidget.connect(self.menubar.closeEvent (),QtCore.SIGNAL('triggered()'), self.closeEvent())

    I get the error:

    closeEvent(QCloseEvent), not enough arguments

    I have the following questions:

    1. I am defining the function incorrectly to capture the closeEvent()?

    2. Do I need a connect statement?

    I have been using PyQt for only 6 weeks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't override the closeEvent()

    Where are you defining the closeEvent() method?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Location
    Fort Wayne, Indiana, USA
    Posts
    8
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: Can't override the closeEvent()

    Thank you for the quick reply. I define the closeEvent as a member function in my window class called Ui_MainWin.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't override the closeEvent()

    closeEvent() is a method of QWidget so it must be defined in a subclass of QWidget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Location
    Fort Wayne, Indiana, USA
    Posts
    8
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: Can't override the closeEvent()

    Thank you for the suggestion. I wrote code for a new class to override the closeEvent() function. I used it as a nested class inside my window class Ui_MainWin:

    class setExit(QtGui.QWidget):
    def closeEvent(self,event):
    print "in routine"

    In my Ui_MainWin class, I create an object for this class:

    self.exitEvent = self.setExit()

    This still didn't work so I have the following questions:

    1. Do I nest my setClass within the Ui_MainWin class or create it as a seperate class?

    2. Is it correct to inherit from the QWidget class? If not, what class do I inherit from?

    3. Does the setExit class need a constructor? If so, what do I set?

    4. The code I use to setup the window is in another module and is generated from Qt designer:

    app = QtGui.QApplication(sys.argv)
    MainWin = QtGui.QMainWindow()
    ui = Ui_MainWin()
    ui.setupUi(MainWin)

    My problem is that the on-line examples I've seen use QWidget instead of QMainWindow to setup the class. I do realize now from your comment that the QMainWindow() doesn't have a closeEvent().

    Also in the window class, setupUi() function, the following code is used to create the cental widget on the window:

    self.centralwidget = QtGui.QWidget(MainWin)

    I realize that the questions I'm asking are probably stupid. Thank you for the help.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can't override the closeEvent()

    You still don't understand. You can't expect some arbitrary instance of an arbitrary subclass of QWidget to receive any possible closeEvent. If you click the X button on window "A" it is the object representing the window "A" that will receive the event and not any other. You have to subclass QMainWindow and deploy your Ui_MainWin class on an instance of this subclass. And the event needs to be reimplemented in the exact same class/object.

    In C++ it would look like this (my Python is rusty so I won't even try writing Python code now):
    Qt Code:
    1. class MainWindow : public QMainWindow {
    2. public:
    3. MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
    4. ui = new Ui::MainWin;
    5. ui->setupUi(this);
    6. }
    7. ~MainWindow(){ delete ui; }
    8. protected:
    9. void closeEvent(QCloseEvent *event) { event->ignore(); } // always prevent the window from being closed
    10. Ui::MainWin *ui;
    11. };
    12. // ...
    13. MainWindow window;
    14. window.show();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2010
    Location
    Fort Wayne, Indiana, USA
    Posts
    8
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: Can't override the closeEvent()

    Thank you for your help

  8. #8
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't override the closeEvent()

    if you are just trying to capture the close event from a user clicking the 'x' button then

    Qt Code:
    1. def closeEvent(self, event):
    2. print ("inside the close")
    To copy to clipboard, switch view to plain text mode 

    is going to work, it'll run through whatever you've got there, but the window is still going to close on you. You are just intercepting it before it actually closes.

    if you need to ask if they really want to close, then you need to interrupt that event being passed in.

    so more like:

    Qt Code:
    1. def closeEvent(self, event):
    2. if checkstatement is True: #(don't really want to close the window
    3. event.ignore()
    4. else:
    5. print ("really closing for real this time!")
    To copy to clipboard, switch view to plain text mode 

    as for connecting your actions to the close event, I typically just connect it the action to self.close as it will also call the closeEvent()

    Hope that helps.

Similar Threads

  1. CloseEvent of QDockWidget
    By mstegehu in forum Qt Programming
    Replies: 5
    Last Post: 16th March 2010, 13:10
  2. QDialog and closeEvent
    By ricardo in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2009, 02:07
  3. QSettings and closeEvent()
    By vito49 in forum Newbie
    Replies: 2
    Last Post: 13th October 2008, 16:18
  4. closeEvent help needed
    By sgmurphy19 in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 17:51
  5. closeEvent
    By jochen_r in forum Newbie
    Replies: 7
    Last Post: 16th January 2006, 12:05

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.