[PyQt] How can i capture activated signal of Tray Icon??

Hans-Peter Jansen hpj at urpla.net
Wed Nov 10 16:36:32 GMT 2010


On Wednesday 10 November 2010, 16:30:03 Jebagnana Das wrote:
> Thanks pete for your reply.
>
> I think you've misunderstood the core of the problem. The problem
> here is not the elif construct(Even changed the code, had no effect).

Well, it might be a poor answer to a poorly formulated question then?
Anyway..

> If we double click on the tray icon, on completion of the first click
> this function gets called, and once again it's called when you finish
> your second click.. Any solutions...

Ah, now I understand your problem. The Trigger event is always thrown, 
even in the DoubleClick case. Hmm, now you found out, why the Trolls 
didn't call the former event just LeftClick ;-), and fixing this would 
shift the issue into DoubleClick handling.

Here's, how I would solve such an issue:

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

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.trayIcon = QSystemTrayIcon(QIcon("some.png"), self)
        self.trayIcon.activated.connect(self.onTrayIconActivated)
        self.trayIcon.show()
        self.disambiguateTimer = QTimer(self)
        self.disambiguateTimer.setSingleShot(True)
        self.disambiguateTimer.timeout.connect(
                self.disambiguateTimerTimeout)

    def onTrayIconActivated(self, reason):
        print "onTrayIconActivated:", reason
        if reason == QSystemTrayIcon.Trigger:
            self.disambiguateTimer.start(qApp.doubleClickInterval())
        elif reason == QSystemTrayIcon.DoubleClick:
            self.disambiguateTimer.stop()
            print "Tray icon double clicked"

    def disambiguateTimerTimeout(self):
        print "Tray icon single clicked"


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())


Pete

> Am Tuesday 09 November 2010 15:08:29 schrieb Jebagnana Das:
> > > Thanks Zoltan.. It did help a lot.. But if i want to capture
> > > single click and double click events of the mouse separately i'm
> > > afraid this can't be used..
> > >
> > > def onTrayIconActivated(self, reason):
> > >         if reason == QSystemTrayIcon.DoubleClick:
> > >             print('tray icon double clicked')
> > >
> > >         if reason == QSystemTrayIcon.Trigger:
> > >             print("Tray icon single clicked")
> > >
> > > If i double click the tray icon it executes both if conditions.
> > > Any way
> >
> > of
> >
> > > resolving this?. Thanks again..
> >
> > elif instead of second if perhaps?
> >
> > Pete
> >
> >
> > ------------------------------
> >
> > _______________________________________________
> > PyQt mailing list
> > PyQt at riverbankcomputing.com
> > http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> >
> > End of PyQt Digest, Vol 76, Issue 18
> > ************************************




More information about the PyQt mailing list