Results 1 to 8 of 8

Thread: [PyQt4] Want to connect a window's close button

  1. #1
    Join Date
    May 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [PyQt4] Want to connect a window's close button

    I want to connect the "x" button on my program's main window with a function that I created. I know how to do it in wxPython:
    Qt Code:
    1. self.Bind(wx.EVT_CLOSE, self.onQuit)
    To copy to clipboard, switch view to plain text mode 

    But I can't figure out how to do it with PyQt4. I've tried:
    Qt Code:
    1. self.connect(self, Qt.SIGNAL('quit()'), self.onQuit)
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. self.connect(self, Qt.SIGNAL('destroyed()'), self.onQuit)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Want to connect a window's close button

    Figured it out with the help of http://zetcode.com/tutorials/pyqt4/firstprograms/

    I just had to rename my function to "closeEvent", and then connect to it:
    Qt Code:
    1. self.connect(self, Qt.SIGNAL('triggered()'), self.closeEvent
    2.  
    3. def closeEvent(self, event):
    4. print "Closing"
    5. self.destory()
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    1

    Default Re: [PyQt4] Want to connect a window's close button

    Is there a way to differentiate closing the window and exiting the application?

    When about to close the main window I want to prompt the user to save the document if the application is about to be closed, however, there are times when my main window runs within another app, and when that happens I don't want to close the document I want to more or less hide the window (so it can be shown again later).

    I believe the application is set to the default exit when all windows are closed but since I'm using the closeEvent to do the document save check, as far as I can tell I have a chicken and the egg situation

  4. #4
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Want to connect a window's close button

    Quote Originally Posted by chezifresh View Post
    Is there a way to differentiate closing the window and exiting the application?

    When about to close the main window I want to prompt the user to save the document if the application is about to be closed, however, there are times when my main window runs within another app, and when that happens I don't want to close the document I want to more or less hide the window (so it can be shown again later).

    I believe the application is set to the default exit when all windows are closed but since I'm using the closeEvent to do the document save check, as far as I can tell I have a chicken and the egg situation
    You could have the close event check QApplication.topLevelWidgets() to see if only one window is returned, ie the widget your checking from.

  5. #5
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Want to connect a window's close button

    Quote Originally Posted by fifth View Post
    You could have the close event check QApplication.topLevelWidgets() to see if only one window is returned, ie the widget your checking from.
    Thought I'd test this and works well. A quick example (with no error checking or exception handling etc);

    Qt Code:
    1. import sys
    2. from PyQt4.QtCore import *
    3. from PyQt4.QtGui import *
    4.  
    5. class SubWindow(QWidget):
    6. def __init__(self, parent = None):
    7. super(SubWindow, self).__init__(parent)
    8. label = QLabel("Sub Window", self)
    9.  
    10. def closeEvent(self, event):
    11. self.deleteLater()
    12. event.accept()
    13.  
    14. class MainWindow(QWidget):
    15. def __init__(self, parent = None):
    16. super(MainWindow, self).__init__(parent)
    17. openButton = QPushButton("Open Sub Window", self)
    18. self.connect(openButton, SIGNAL("clicked()"), self.openSub)
    19.  
    20. def openSub(self):
    21. self.sub = SubWindow()
    22. self.sub.show()
    23.  
    24. def closeEvent(self, event):
    25. widgetList = QApplication.topLevelWidgets()
    26. numWindows = len(widgetList)
    27. if numWindows > 1:
    28. event.ignore()
    29. else:
    30. event.accept()
    31.  
    32. app = QApplication(sys.argv)
    33. mainWin =MainWindow()
    34. mainWin.show()
    35. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    The window count will include any windows which are hidden, ie object created but not deleted. In the example the child windows delete on closing, but depending on your needs you could iterate through the WidgetList to check whats visible etc.
    Last edited by fifth; 13th May 2009 at 13:40.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [PyQt4] Want to connect a window's close button

    did you also read about QWidget::closeEvent?
    ooops, yes, you did.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Apr 2008
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Want to connect a window's close button

    Quote Originally Posted by spirit View Post
    did you also read about QWidget::closeEvent?
    ooops, yes, you did.
    lol yeah, my solution to the op's question from yesterday was in the closeEvent code

  8. #8
    Join Date
    Apr 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Want to connect a window's close button

    If my SubWindow in the above give example is not a separate class and child of the MainWindow how should I use the closeEvent of SUbWindow.

    Thanks

    Jothy

Similar Threads

  1. How to hide Close Button "X" on Widget ?
    By merry in forum Qt Programming
    Replies: 8
    Last Post: 25th January 2020, 08:03
  2. Disable Close button (X) of a QDialog
    By BrainB0ne in forum Qt Programming
    Replies: 29
    Last Post: 8th December 2009, 17:01
  3. QDialog not showing close button on Mac
    By manojmka in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2008, 12:56
  4. Replies: 5
    Last Post: 21st April 2008, 07:54
  5. Replies: 2
    Last Post: 5th February 2007, 17:42

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