Results 1 to 4 of 4

Thread: Keybord shortcuts for hidden widgets / actions

  1. #1
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Keybord shortcuts for hidden widgets / actions

    In menubar I have action "Show menu" which is checkable. For that action I have also shortcut and it works great only when menu is visible. When menubar is not visible then shortcut does not work.

    How I can implement shortcuts which will work with not visible / hidden actions in menubar?
    Thanks,


    Added after 41 minutes:


    Ok, I found solution - reimplement parent QWidget::keyPressEvent(QKeyEvent *event);

    Qt Code:
    1. void MyWidget::keyPressEvent(QKeyEvent *event)
    2. {
    3. if( event->modifiers() == Qt::ControlModifier ) {
    4. if( event->key() == Qt::Key_M )
    5. // show my menubar
    6. }
    To copy to clipboard, switch view to plain text mode 

    but now second question - is it the best solution for that purpose?
    Last edited by Scope; 3rd April 2016 at 16:57.

  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: Keybord shortcuts for hidden widgets / actions

    For a global shortcut I would go for a global event filter, to be sure you catch it independent of which widget has focus.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2015
    Posts
    41
    Thanks
    5
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Keybord shortcuts for hidden widgets / actions

    anda_skoa you mean something like that:

    main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3.  
    4. QApplication app(argc, argv);
    5. MyWidget w;
    6. app.installEventFilter(&w);
    7. w.show();
    8.  
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    event filter in MyWidget
    Qt Code:
    1. bool MyWidget::eventFilter(QObject *object, QEvent *event)
    2. {
    3. if ( event->type() == QEvent::KeyPress) {
    4. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    5. if( keyEvent->modifiers() == Qt::ControlModifier ) {
    6. switch (keyEvent->key()) {
    7. case Qt::Key_M:
    8. ui->actionShow_menu->setChecked(!ui->actionShow_menu->isChecked());
    9. return true;
    10. case Qt::Key_Left:
    11. prevPage();
    12. return true;
    13. case Qt::Key_Right:
    14. nextPage();
    15. return true;
    16. }
    17.  
    18. return false;
    19. }
    20. }
    21.  
    22. return QObject::eventFilter(object, event);
    23. }
    To copy to clipboard, switch view to plain text mode 

    This way is correctly?

    Next question, in eventFilter If I want to have "global event handler" I do not need check which object send current event, I am right? So I do not need line:

    Qt Code:
    1. if( object == someWidget ) {
    2. // task
    3. }
    To copy to clipboard, switch view to plain text mode 
    Thanks,

  4. #4
    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: Keybord shortcuts for hidden widgets / actions

    Yes, that looks good, though your switch() should have a default.

    And indeed, a global shortcut should work independent of which widget the key would have originally been sent to.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Scope (6th April 2016)

Similar Threads

  1. Replies: 6
    Last Post: 13th May 2011, 08:38
  2. The delegated widgets become hidden
    By fulbay in forum Qt Programming
    Replies: 4
    Last Post: 20th December 2010, 12:53
  3. widgets behind hidden widgets not working
    By bpetty in forum Newbie
    Replies: 13
    Last Post: 7th September 2007, 20:23
  4. Spacing widgets and actions in a toolbar
    By indifference in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2007, 22:59
  5. Key shortcuts for not visible widgets
    By trskel in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 10:02

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.