Important: Please read the Qt Code of Conduct -

Sending key shortcuts to active window

  • Hello guys, I do wonder, what's the best way to send shortcuts to active window? I do have menuBar which emits slots in my "shortcut dispatcher" object. Now I do wonder, how to push forward those messages forward? Hook up focusEvent and cast QWidget to my own Window subclass?

  • Moderators

    Why do you "dispatch shortcuts" yourself? Qt has a built in mechanism for shortcuts and scoping.
    If you have a QShortcut with a certain widget parent or a QAction with shortcut added to that widget and you set the context to Qt::WidgetWithChildrenShortcut it will be scoped properly without you doing anything more.

  • Well, I'm dispatching between multiple windows, hence no common parent is present...I want to have shortcuts active only for active window.

  • Moderators

    I'm not sure I understand. If you have shortcut shA with parent window A and shortcut shB with parent window B and both shortcuts have a Qt::WidgetWithChildrenShortcut scope then they work in their respective windows. The windows don't need a common parent for shortcuts to work.

  • Well, I have a custom shortcut shC applied to Action/Menu which is global (in every window), but I can not register that into window A, or window B. I mean, I am on OS X, where there is only one menubar that is shared across the multiple windows, so I have to dispatch them, right?

    I mean, for example, Qt Creator has a shortcut "Run" which is invokable from many windows. But I can not make two menubars and/or having two actions, right?

Log in to reply