1

I would like to implement keyboard shortcuts for my QGraphicsScene. My graphical objects are derived from QGraphicItem and QObject, so I can use signal/slot connections.

I'm already using QActions for context menus and now I would like to use some of QActions also as actions for keyboard shortcuts on the selected item.

My QGraphicsItems have enabled ItemIsFocusable and ItemIsSelectable via setFlag();

I can receive keyPressEvent(QKeyEvent* event) but in such case I would have to manually test event->key() == Qt::Key_xxx

Is there any way how to do this automatically?

  • I tried to compare QKeyEvent with QKeySequence, but this doesn't work (because sequence can contain multiple keys).

Thanks for any help

Ludek Vodicka
  • 1,610
  • 1
  • 18
  • 33

1 Answers1

0

I'm not entirely sure what you mean by automatically. I'm assuming you're just trying to avoid having to manually check the key, which unfortunately I'm not sure of any simpler method.

Catching the QKeyEvent* event like you're doing is generally the way to go, and yes unfortunately once you have the event you'll need to make sure it's expected. You may be able to make the code less ugly by using a bitmask or similar logic operations, but in general I've used a switch on event->key() and that tends to keep things readable.

dabbler
  • 404
  • 3
  • 8
  • And how do you handle multi-keyboard shortcuts offered by QKeySequence? What I would like to see some kind of handler (in scene/parent widget) where I can compare QKeySquence of QAction with QKeySequence received from user input. – Ludek Vodicka Oct 08 '19 at 05:06
  • Ah, sorry, I missed that you were trying to capture QKeySequence, not just QKeyEvent. The method is similar, but you'll want to check for `QEvent::ShortcutOverride` in your conditional. See the following exchange for more information, hopefully this helps you out: https://forum.qt.io/topic/34583/solved-qwidget-eventfilter-not-catching-key-combinations/3 – dabbler Oct 08 '19 at 17:48