[PyQt] Detecting clicks on Mac Dock

Yao Ko koyao at raptr.com
Wed Mar 3 02:13:25 GMT 2010


Hi,

This is a Mac-specific question:

Is it possible to detect clicks on the dock icon of a QApplication?  I
tried adding debug messages on QApplication.event() and it only gets
triggered when the application is activated or deactivated.  But if
the user clicks on the dock when the app is already activated, it
doesn't trigger any more events.

Sample code:

"""
import sys
from PyQt4 import QtGui, QtCore

class MyApp(QtGui.QApplication):
  def __init__(self):
    QtGui.QApplication.__init__(self, sys.argv)
    QtGui.QApplication.setQuitOnLastWindowClosed(False)

  def event(self, e):
    print 'Inside event(): %s' % e.type()
    return QtGui.QApplication.event(self, e)

def main():
  app = MyApp()
  sys.exit(app.exec_())

if __name__ == '__main__':
  main()
"""

Running it:

$ python dock_example.py
Inside event(): 67
Inside event(): 70
Inside event(): 70
Inside event(): 70
Inside event(): 121      <- QEvent.ApplicationActivate

(and if I continue click on the dock icon, it doesn't trigger any more events).

The goal is to show() and raise() the QApplication whenever someone
clicks on the dock icon, even though if the main window is hidden.

Thanks,
Yao


More information about the PyQt mailing list