Results 1 to 4 of 4

Thread: Keyboard shortcut event not received

  1. #1
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    4
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Keyboard shortcut event not received

    I have a custom subclass of QGraphicsScene that executes a context menu. The context menu works, but I can't seem to receive any keyboard shortcuts for it. Here's a simplified example of my code.
    Qt Code:
    1. MyScene::MyScene(QObject *parent) :
    2. {
    3. m_contextMenu = new QMenu();
    4. QAction *action = m_contextMenu->addAction(tr("Duplicate"), this);
    5. action->setShortcut(tr("Ctrl+D"));
    6. connect(action, SIGNAL(triggered(), this, SLOT(duplicateSelectedItem()));
    7. }
    To copy to clipboard, switch view to plain text mode 

    The duplicateSelectedItem() slot never gets called. How do I ensure the keyboard shortcut events get propagated to this action? I got the impression this would happen when the QGraphicsView gets focus, but clearly it can't figure out that the actions I've created in the scene are in focus. If it makes any difference, I'm testing this on OSX. Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Keyboard shortcut event not received

    Look that the menu is in no way tied to the graphics view so pressing keys when the view has focus will not lead towards the appropriate actions. In your situation it is best to build the context menu differently. Instead of creating a custom QMenu object and adding actions to it, use the Qt::ActionsContextMenu context menu policy and use addAction() to add the actions directly to the view widget. Then all the shortcuts will be active the whole time the widget has focus and in addition if you right click the view, a context menu made of actions you added to the widget will be created for you. If you don't really want the context menu, just the shortcuts then don't set the context menu policy to ActionsContextMenu and only add the actions to the widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    D Cell (15th December 2010)

  4. #3
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    4
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Keyboard shortcut event not received

    Thanks. I got your suggestion to work. I also had to set
    Qt Code:
    1. action->setShortcutContext(Qt::WidgetShortcut);
    To copy to clipboard, switch view to plain text mode 
    since I have more than one instance of the view. Otherwise I got an error about an ambiguous shortcut.

    For future reference, is it not possible then to receive keyboard shortcuts on anything except widgets? Is calling QWidget::addAction(...) the only way to setup the keyboard shortcuts to propagate correctly?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Keyboard shortcut event not received

    Quote Originally Posted by D Cell View Post
    For future reference, is it not possible then to receive keyboard shortcuts on anything except widgets? Is calling QWidget::addAction(...) the only way to setup the keyboard shortcuts to propagate correctly?
    The action has to be tied to something that is currently visible. It is possible to hack a global shortcut support by catching shortcuts in the application object and triggering appropriate actions manually but it's not available out of the box.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 23rd August 2010, 10:37
  2. keyboard event transfer
    By vijayabcdef in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2010, 06:54
  3. Additional keyboard shortcut for a menu
    By negritot in forum Qt Programming
    Replies: 5
    Last Post: 17th September 2009, 05:58
  4. Invoke QCompletion on keyboard shortcut
    By aspidites in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2009, 10:32
  5. Rerouting messages for keyboard shortcut selection dialog.
    By andy.fillebrown in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.