PDA

View Full Version : [PyQt4] Want to connect a window's close button



Dodle
11th May 2009, 00:33
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:

self.Bind(wx.EVT_CLOSE, self.onQuit)


But I can't figure out how to do it with PyQt4. I've tried:

self.connect(self, Qt.SIGNAL('quit()'), self.onQuit)
and

self.connect(self, Qt.SIGNAL('destroyed()'), self.onQuit)

Dodle
11th May 2009, 01:17
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:

self.connect(self, Qt.SIGNAL('triggered()'), self.closeEvent

def closeEvent(self, event):
print "Closing"
self.destory()

chezifresh
12th May 2009, 18:52
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

fifth
13th May 2009, 00:07
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() (http://doc.trolltech.com/4.5/qapplication.html#topLevelWidgets) to see if only one window is returned, ie the widget your checking from.

fifth
13th May 2009, 13:34
You could have the close event check QApplication.topLevelWidgets() (http://doc.trolltech.com/4.5/qapplication.html#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);


import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class SubWindow(QWidget):
def __init__(self, parent = None):
super(SubWindow, self).__init__(parent)
label = QLabel("Sub Window", self)

def closeEvent(self, event):
self.deleteLater()
event.accept()

class MainWindow(QWidget):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
openButton = QPushButton("Open Sub Window", self)
self.connect(openButton, SIGNAL("clicked()"), self.openSub)

def openSub(self):
self.sub = SubWindow()
self.sub.show()

def closeEvent(self, event):
widgetList = QApplication.topLevelWidgets()
numWindows = len(widgetList)
if numWindows > 1:
event.ignore()
else:
event.accept()

app = QApplication(sys.argv)
mainWin =MainWindow()
mainWin.show()
sys.exit(app.exec_())

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.

spirit
13th May 2009, 13:38
did you also read about QWidget::closeEvent?
ooops, yes, you did. :)

fifth
13th May 2009, 15:11
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 :p

jothy
21st April 2010, 11:13
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