Results 1 to 3 of 3

Thread: QAction with Escape shortcut (Qt::Key_Escape)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default QAction with Escape shortcut (Qt::Key_Escape)

    Hi,

    I'm implementing a context menu in QWidget derived class and I want an action to be triggered by Escape shortcut, so I set a shortcut when I create QAction:

    Qt Code:
    1. m_clearBlockAction = new QAction(tr("Clear selection"), this);
    2. m_clearBlockAction->setShortcut(tr("Esc"));
    3. addAction(m_clearBlockAction);
    To copy to clipboard, switch view to plain text mode 

    The problem is shortcut does not work and even more - it intercepts (or does not allow to fire) key stroke events, i.e. even if I put direct handling into event filter, it does not catch Qt::Key_Escape code:

    Qt Code:
    1. bool WidgetCtrl::event(QEvent *e)
    2. {
    3. if (e->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(e);
    6.  
    7. if (keyEvent->key() == Qt::Key_Escape)
    8. {
    9. qDebug() <<"ESC";
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    qDebug() <<"ESC" is never gets executed; once I remove shortcut from action, event code works.


    Any help?
    Thanks
    Serge T

  2. #2
    Join Date
    Jan 2012
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QAction with Escape shortcut (Qt::Key_Escape)

    This is how I would solve your problem:
    Qt Code:
    1. QAction* pAction = new QAction( "text4ESC", this );
    2. pAction->setShortcut( Qt::Key_Escape );
    3. pAction->setShortcutContext( Qt::WindowShortcut );
    4. connect( pAction, SIGNAL(triggered()),
    5. this, SLOT(SL_DoSomething()) );
    6. addAction( pAction );
    7.  
    8. setContextMenuPolicy( Qt::ActionsContextMenu );
    To copy to clipboard, switch view to plain text mode 
    If you add this to the constructor of your widget, it should work. I guess the shortcut context is important...

    To solve the problem with the event() method, you could install an event filter on all widgets - since (I guess) the focus widget gets the key events first, and if it accepts them, they were not forwarded to the parent widgets.

  3. #3

    Default Re: QAction with Escape shortcut (Qt::Key_Escape)

    Thank you for your reply. Actually it was my inattention - the slot DID was called by the trigger, no problem. However, what confused me is the fact that both event() and keyPressedEvent() stop receiving Esc once I assigned an action shortcut to it. I expected event() to always process all keystrokes.

    As for ShortcutContext, it is not necessary to set since it is Qt::WindowShortcut alreadty by default.

    Serge T

Similar Threads

  1. checkable QAction shortcut hold
    By winder in forum Qt Programming
    Replies: 7
    Last Post: 18th February 2010, 15:48
  2. QAction shortcut
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2009, 20:35
  3. Trigger QAction by 'Key_Escape'
    By Raccoon29 in forum Newbie
    Replies: 5
    Last Post: 3rd April 2008, 10:24
  4. XML escape
    By Vladimir in forum Qt Programming
    Replies: 6
    Last Post: 19th July 2007, 18:59
  5. Escape Sequences in Qt
    By Kofie in forum Newbie
    Replies: 1
    Last Post: 21st March 2007, 10:33

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
  •  
Qt is a trademark of The Qt Company.