Your Cocoa Build of Qt and You

Interestingly, I had to start building my apps on OSX because the apps I built on my Snow Leopard machine stopped working (loader error and other good times).

After much suffering my boss set up a working Qt/PyQt/PIL install on his OSX machine. I ditched my beloved Snow Leopard deployment script and cobbled together a new one. I apped remotely. All was OK.

But funny thing.

My application’s keyboard shortcuts stopped working.

To be more specific, my keyboard shortcuts modified with the COMMAND button stopped working. The same keyboard shortcuts modified with the CTRL button continued to work.

I overload my widget’s keyPressEvent() method like so:

def keyPressEvent(self, event):

isCommandButton = (event.modifiers() & QtCore.Qt.ControlModifier)
isCtrlButton = (event.modifiers() & QtCore.Qt.MetaModifier)
isCtrlOrCommand = isCtrlButton or isCommandButton

(Yes, on Mac the MetaModifier corresponds to the CTRL button and the ControlModifier corresponds to the COMMAND button. Yes, this is confusing.)

So when COMMAND+9 stopped doing what COMMAND+9 used to do, I dug a little deeper. Was the COMMAND button not sending the expected value?

It was. That wasn’t the problem. This is the problem:

CTRL+9 (works)

keyPressEvent! key: 16777250, text:
- Was that a ControlModifier? False
- Was that a MetaModifier? True
keyPressEvent! key: 57, text:
- Was that a ControlModifier? False
- Was that a MetaModifier? True

That makes sense, because the first keyPressEvent! happens when CTRL is pressed, and the second when 9 is pressed.

CMD+9 (does not work)

keyPressEvent! key: 16777249, text:
- Was that a ControlModifier? True
- Was that a MetaModifier? False

Only one keyPressEvent! I can mash the “9” button all day long and no keyPressEvent occurs.

It is like pressing the COMMAND button blocks any other key event.

What. The hey.

I read around and I figure the problem is some difference between the Qt Carbon (what I have locally on Snow Leopard) and Cocoa (what my boss has on his OSX) builds. A relevant bug:

On Mac OS X 10.6.4 with Qt 4.7.0 beta 2 Cocoa I found that Ctrl+Tab and Shift+Tab are not caught by an event filter on the qApp object. Tab and Alt+Tab pose no problems. It does not seems to me that the system intercept these key combinations (I’ve checked in System Preferences/Keyboard for the assigned shortcuts).

Mac cocoa only: QKeyEvent Qt::Key_Tab not accessible to event or eventFilter

Similarly, CMD+C doesn’t copy, CMD+V doesn’t paste, etc. My users miss those features. They miss them so.

My end run is, in Qt Designer, to add menu actions:


You may now gasp at my hacking overkill. But I really don’t know how else to get around it. I’m startled to madness that the menu actions work at all.


Wait a minute.

Shortcuts use event filters.

Event filters happen before keyPressedEvent()s get called.

I need an application-wide event filter.

Will update.


I couldn’t make EventFilter work — either by setting new values on the received event or trapping it and posting a new one. The former didn’t stick and the latter caused an event loop.

I also tried to overload the QApplication event() method, but the problem is that once COMMAND is depressed no further key events are captured, so even the app event() method is really too late.

So I wrote a menu shortcut for every shortcut in my application.

Yes, I did.

(105 visits, 1 today)