[Qt-interest] HowTo: set application-wide shortcuts without associated Widget?

kent williams nkwmailinglists at gmail.com
Fri Feb 25 19:15:10 CET 2011


I would like to have some keyboard shortcuts in my application that
aren't associated with a menu item or pushbutton or other widget.
Specifically I have 3 sliders and want to change their values based on
keystrokes. For example Ctrl+K moves one of the sliders left, Ctrl+L
moves it right, with other pairs of keys controlling the other two
sliders.

What I tried was this:

void
QImageEval2Window::
AddKeyCommand(const char *name,QKeySequence &sequence)
{
  QAction *action(new QAction(this));
  action->setObjectName(name);
  action->setShortcut(sequence);
  // changeSlice() uses the objectName to decide which
  // slider to move which direction.
  connect(action,SIGNAL(triggered()),
          this,SLOT(changeSlice()));
}

So I create actions, they have accellerators, but they aren't part of
a menu or toolbar.

I could make this work by adding the actions a menu, or actions to a
toolbar, but it seems like there should be some way to set some global
keyboard shortcuts, the way Control+Q is the application-wide 'quit'
shortcut.



More information about the Qt-interest-old mailing list