Can't catch hotkey in my own widget with Maya and Qt

Hello,

I wrote a mll plugin for maya and use qt in it (I use maya 2011 x64 on windows and Qt opensource 4.5.3, build with vs2008).

There’s a QMainFrame in my plugin and some actions with shortcut added to the widget.

When I run it in maya, I can’t use any hotkey to invoke the my actions. For example, I set the Ctrl+n to my own “new file” action, but after I pressed Ctrl+n in maya (I focused on my widget), it just made a new maya scene, not my “new file” action. Then I tried to change the hotkey which isn’t existing in maya, like Ctrl+e, but failed also.

But I start my widget in standalone QApplication, I can use all my hotkeys!

So I can’t catch the hotkey in maya any more.

How to fix it?
Thx!

I have no idea in Maya, but in Max, you needed to disable key acceleration. Look over the API for some way to disable hotkeys.

doing a setFocusPolicy(Qt::StrongFocus) on the widget might help

I too am currently struggling with this issue. Looking at the setFocusPolicy as suggested doesn’t solve it. I suspect it has something to do with Maya hijacking the shortcut signals. Afterall, Maya’s UI is now using the same Qt shortcut system.

I am going to try the QKeyEvent directly and see how that goes…

You will probably have to install an eventFilter on the qApp to catch them, Maya is weird about event handling versus the way Qt works by default.

As I suspected, you have reimplement the keyPressEvent for each widget you want to specify hotkeys for.

[pseudocode]

class Window():
   def __init__():
      # this assumes using uic to load a ui file
      # that has centralwidget as the main window
      # and tableview as a child

       self.centralwidget.setFocusPolicy(qt.StrongFocus)
       self.centralwidget.keyPressEvent = self.mainWindowKeyEvent
       self.tableview.keyPressEvent = self.tableKeyEvent

    def mainWindowKeyEvent(self, event):
        pass
        # this just accepts all keystrokes and does nothing with them
        # so that they don't get propagated on to Maya's hotkeys

    def tableKeyEvent(self, event):
        if event.key() == Qt.[keystroke]:
            [do something interesting]
            event.accept()  # make sure the event stops here

        else:
             # make sure any usual tableview hotkeys still get dealt with
             QtGui.QTableView.keyPressEvent(self.tableview, event)