3

I'm having trouble understanding the shortcut functionality of a QAction added to a QMenu. Lets start with an example:

from PyQt4 import QtCore, QtGui
import sys

class TestApp(QtGui.QMainWindow):
    def __init__(self, *args):
        super(TestApp, self).__init__(*args)

        #create contex menu
        self.menu = QtGui.QMenu(self)
        self.menu.addAction("testEntry", self.action, "CTRL+T")

        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.execContextMenu)

    def execContextMenu(self, point):
        self.menu.exec_(self.mapToGlobal(point))

    def action(self):
        print 'action called!'

app = QtGui.QApplication(sys.argv)
win = TestApp()
win.show()

app.exec_()

I would expect action() to be called either when the users presses "Ctrl+T" or when "Ctrl+T" is pressed after the context menu was invoked. But nothing happens when I press the shortcut. However when I add the QAction to the TestApp

act = self.menu.addAction("testEntry", self.action, "CTRL+T")
self.addAction(act)

it works (although it doesn't when the context menu is open). So what am I doing wrong?

I'm using PyQt4 on an OSX 10.6

Thanks for any help ;)

voocoder
  • 31
  • 1
  • 2

1 Answers1

1

On Mac Qt maps the control key to Meta rather than Ctrl

Jelle
  • 690
  • 4
  • 13