2

I am new in Qt (PyQt) and I am trying to make an app whose functions will be executed from menubars/system trays. A perfect example is show here:

enter image description here

I cannot find a good resource on how I can do this. Can someone advice.

Thanks.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Cheruiyot Felix
  • 1,557
  • 5
  • 26
  • 42
  • Not yet. I started learning and doing most of my stuff through the qt designer.Looking for the resource, does it provide this feature? I will be glad. – Cheruiyot Felix May 03 '14 at 06:35

1 Answers1

1

I think you are looking for working with QMenu and QMainWindow for the menu part, at least.

Here you can find a C++ example:

Menus Example

and here a PyQt4 example:

Menus and Toolbars in PyQt4

Here is the code inline for your convenience:

import sys
from PyQt4 import QtGui

class Example(QtGui.QMainWindow):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):               

        exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)        
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(QtGui.qApp.quit)

        self.statusBar()

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Menubar')    
        self.show()


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

As for the QSystemTrayIcon part, you could write something like this:

def main():
    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon("Bomb.xpm"), w)
    menu = QtGui.QMenu(parent)
    exitAction = menu.addAction("Foo")
    trayIcon.setContextMenu(menu)

    trayIcon.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • 1
    I went through the link sometime earlier and tried but my problem is, will it dock properly like the one in dropbox without the main window being shown. Most of my task will be background process. So I just need the shortcut, and when the machine is started, it shows there and user can use the menu to execute different tasks.No main window. More like a jumplist or something.I cannot find a better word i.e what it is called. – Cheruiyot Felix May 03 '14 at 06:45
  • Have no clue what you are talking about. :) You are showing an application (at least to me), and this is the way of doing it. There are no global shortcuts in Qt proper, what-so-ever. They can be even dangereous, so I am not sure it is a really good idea. – László Papp May 03 '14 at 06:48
  • I think I have to read on QMenu and the Bars. This question takes me close to that: http://stackoverflow.com/questions/893984/pyqt-show-menu-in-a-system-tray-application?rq=1 – Cheruiyot Felix May 03 '14 at 07:04
  • I tried the code and it works. I need to leverage on the QSystemTrayIcon api. I think I should go ahead and answer the above question. Any other better idea? – Cheruiyot Felix May 03 '14 at 07:13
  • @KiuFelix: I updated my answer with a slightly different, but shorter, and IMHO better approach :) – László Papp May 03 '14 at 07:32
  • Hey, thanks for the update. I will take this as the answer. The approach is nice, I learnt something. – Cheruiyot Felix May 03 '14 at 19:25