If you try to use the python standard unix signal handlers with QCoreApplication the handler never gets called. Comment out the creation(and exec_) call, the signal handler gets called as expected.

So, I'm forced to use the unixSignal qt SIGNAL of QCoreApplication but I can't get it to actually trap the SIGHUP.

Qt Code:
  1. import sys
  2. import time
  3. from PyQt4 import QtCore
  4.  
  5. HUP_EVENT = QtCore.QEvent.User + 1
  6.  
  7.  
  8. class Application(QtCore.QCoreApplication):
  9. def __init__(self):
  10. QtCore.QCoreApplication.__init__(self, sys.argv)
  11.  
  12. self.connect(self, QtCore.SIGNAL('unixSignal(int)'), self.onUnixSignal)
  13. self.connect(self, QtCore.SIGNAL('sigHUP()'), self.onSIGHUP)
  14.  
  15.  
  16. def onUnixSignal(self, signum):
  17. print 'unix signal'
  18. if signum == signal.SIGHUP:
  19. self.postEvent(self, QtCore.QEvent(HUP_EVENT))
  20.  
  21. def event(self, event):
  22. print 'In event, %s' % event.type()
  23. if event.type() == 1:
  24. self.emit(QtCore.SIGNAL("sigHUP()"))
  25. return True
  26.  
  27. return False
  28.  
  29. def onSIGHUP(self):
  30. print 'in onsighup'
  31. self.quit()
  32.  
  33.  
  34. if __name__ == '__main__':
  35. app = Application()
  36. sys.exit(app.exec_())
  37. print 'Done'
To copy to clipboard, switch view to plain text mode 

Prints

In event, 67
Hangup
Which shows the unix signal was not trapped.

This shows PyQt trashes the python signal module handlers

Qt Code:
  1. import signal
  2. import sys
  3. import time
  4. from PyQt4 import QtCore
  5.  
  6. HUP_EVENT = QtCore.QEvent.User + 1
  7.  
  8.  
  9. def sighup_handler(signum, frame):
  10. print 'python signal handler'
  11. self.postEvent(self, QtCore.QEvent(HUP_EVENT))
  12.  
  13.  
  14.  
  15. class Application(QtCore.QCoreApplication):
  16. def __init__(self):
  17. QtCore.QCoreApplication.__init__(self, sys.argv)
  18.  
  19. self.connect(self, QtCore.SIGNAL('sigHUP()'), self.onSIGHUP)
  20.  
  21.  
  22. def event(self, event):
  23. print 'In event, %s' % event.type()
  24. if event.type() == HUP_EVENT:
  25. self.emit(QtCore.SIGNAL("sigHUP()"))
  26. return True
  27.  
  28. return False
  29.  
  30. def onSIGHUP(self):
  31. print 'in onsighup'
  32. self.quit()
  33.  
  34.  
  35. if __name__ == '__main__':
  36. app = Application()
  37.  
  38. signal.signal(signal.SIGHUP, sighup_handler)
  39.  
  40. sys.exit(app.exec_())
  41.  
  42. print 'Done'
To copy to clipboard, switch view to plain text mode 

Prints

In event, 67
and then nothing.

Comment out the instantiation of the app and...

Qt Code:
  1. import signal
  2. import sys
  3. import time
  4. from PyQt4 import QtCore
  5.  
  6. HUP_EVENT = QtCore.QEvent.User + 1
  7.  
  8.  
  9. def sighup_handler(signum, frame):
  10. print 'python signal handler'
  11. self.postEvent(self, QtCore.QEvent(HUP_EVENT))
  12.  
  13.  
  14.  
  15. class Application(QtCore.QCoreApplication):
  16. def __init__(self):
  17. QtCore.QCoreApplication.__init__(self, sys.argv)
  18.  
  19. self.connect(self, QtCore.SIGNAL('sigHUP()'), self.onSIGHUP)
  20.  
  21.  
  22. def event(self, event):
  23. print 'In event, %s' % event.type()
  24. if event.type() == HUP_EVENT:
  25. self.emit(QtCore.SIGNAL("sigHUP()"))
  26. return True
  27.  
  28. return False
  29.  
  30. def onSIGHUP(self):
  31. print 'in onsighup'
  32. self.quit()
  33.  
  34.  
  35. if __name__ == '__main__':
  36. # app = Application()
  37.  
  38. signal.signal(signal.SIGHUP, sighup_handler)
  39.  
  40. # sys.exit(app.exec_())
  41. time.sleep(30)
  42.  
  43. print 'Done'
To copy to clipboard, switch view to plain text mode 

python signal handler
Traceback (most recent call last):
File "sharedb/app.py", line 41, in <module>
time.sleep(30)
File "sharedb/app.py", line 11, in sighup_handler
self.postEvent(self, QtCore.QEvent(HUP_EVENT))
NameError: global name 'self' is not defined