Results 1 to 2 of 2

Thread: QAction shortcut does not work for QApplication::sendEvent in QT5

  1. #1
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QAction shortcut does not work for QApplication::sendEvent in QT5

    Hi,

    I am adding a QAction to a QWidget and trying to trigger the action through the shortcut set to the QAction, by sending a QKeyEvent using QApplication::sendEvent.
    The action is not triggered on sending the event, but works fine if I press the key from keyboard.
    This happens in qt5.6 but works fine in qt4.6.

    the code looks like,

    Qt Code:
    1. QAction *pAction = new QAction( this );
    2. pAction->setText( tr( "Delete" ) );
    3. pAction->setShortcut( QKeySequence::Delete );
    4. pAction->setShortcutContext( Qt::WidgetWithChildrenShortcut );//tried with other values of the enum also
    To copy to clipboard, switch view to plain text mode 

    I add this action to a widget derived from QWidget.
    Qt Code:
    1. testWidget = new TestWidget();//assume "testwidget" is the pointer holding the TestWidget
    2. testWidget->addActionToWidget( action );//action is added to the widget and connect( action, SIGNAL( triggered() ), SIGNAL( actionInvoked() ) );
    3. testWidget->show();
    4. connect( testWidget , SIGNAL( actionInvoked() ),this, SLOT( actionTriggered() ) );//actionTriggered slot to be called
    5.  
    6. QKeyEvent keyEvent( QEvent::KeyPress, Qt::Key_Delete, Qt::NoModifier );
    7. QApplication::sendEvent( testAction, &keyEvent );
    To copy to clipboard, switch view to plain text mode 

    the slot is not called.

    Am I missing something here, since the same piece of code works fine in the Qt4.6.

    Thanks
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAction shortcut does not work for QApplication::sendEvent in QT5

    Have you tried a qWaitForWindowActive() before sending the event?

    And/or sending the event to the window or application object?

    Cheers,
    _

Similar Threads

  1. QAction shortcut / QShortcut conflict with other widget
    By Alundra in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2015, 03:09
  2. QAction shortcut and text alignment
    By NIteLordz in forum Qt Programming
    Replies: 0
    Last Post: 18th January 2015, 02:47
  3. QAction with Escape shortcut (Qt::Key_Escape)
    By serget in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2012, 16:34
  4. checkable QAction shortcut hold
    By winder in forum Qt Programming
    Replies: 7
    Last Post: 18th February 2010, 15:48
  5. QAction shortcut
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2009, 20:35

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.