Results 1 to 4 of 4

Thread: Shift+F1 only works if a QWhatsThis action has been added to the menu/toolbar

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Shift+F1 only works if a QWhatsThis action has been added to the menu/toolbar

    Does anybody know, why Actions only work, when they have been added to a menu or toolbar in the first place? The same applies to QWhatsThis stuff. pressing Shift+F1 won't work until I have added a QWhatsThis action to the menu ... This may not be a problem in a QMainWindow where toolbars and menus are available most of the times, but it is a problem in dialogs ...

    This way I neither have the opportunity to have Shortcuts (except accelarators ...) to some actions nor, QWhatsThis popups if I don't have a menu or toolbar.

    Thanx in advance
    Momesana

  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: Shift+F1 only works if a QWhatsThis action has been added to the menu/toolbar

    It is because of the way shortcuts are defined by default in QAction. Their shortcut context is not set to Qt::ApplicationShortcut. If you want your actions to be active for the whole application, you have to change the context yourself using QAction::setShortcutContext( Qt::ShortcutContext context ).

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Unhappy Re: Shift+F1 only works if a QWhatsThis action has been added to the menu/toolbar

    Quote Originally Posted by wysota View Post
    It is because of the way shortcuts are defined by default in QAction. Their shortcut context is not set to Qt::ApplicationShortcut. If you want your actions to be active for the whole application, you have to change the context yourself using QAction::setShortcutContext( Qt::ShortcutContext context ).
    First of all, thank you for replying to my post.

    I had tried setShortcutContext() before and just did it again, but it doesn't work. I used all three possible values Qt::ApplicationShortcut, Qt::WindowShortcut and Qt::WidgetShortcut.
    What about the QWhatsThis thing, which is most important to me at this time. I want to call QWidget::setWhatsThis( "..." ) for the widgets I want to provide with whatsthis help and make it possible for the user to do Shift+F1 to get into QWhatsThis mode. Since this does not work out of the box I could get create an Action with QAction *WhatsThisact = QWhatsThis::createAction() and set a shortcut for it so users can get into whatsthis mode by activating the shortcut, but unfortunately that will also not work due to the problems with QActions that I have explained above ...

    Qt Code:
    1. void MainWindow::createActions()
    2. {
    3. prefsAct = new QAction( tr("&Preferences"), this );
    4. prefsAct->setShortcut( tr("Ctrl+P") );
    5. prefsAct->setShortcutContext( Qt::ApplicationShortcut);
    6. connect( prefsAct, SIGNAL( activated() ), this, SLOT( setPreferences() ) );
    7.  
    8. exitAct = new QAction( QIcon(":images/exit.png"), tr("&Exit"), this );
    9. exitAct->setToolTip( tr("Exit the application") );
    10. connect(exitAct, SIGNAL(activated()), qApp, SLOT(quit()));
    11.  
    12. helpAct = new QAction ( QIcon(":images/help.png"), tr("Online Help"), this);
    13. helpAct->setToolTip( tr("Online Help") );
    14. connect( helpAct, SIGNAL(activated()), this, SLOT(help()));
    15.  
    16. aboutAct = new QAction ( tr("About servant"), this);
    17. aboutAct->setToolTip( tr("About servant") );
    18. connect( aboutAct, SIGNAL(activated()), this, SLOT(about()));
    19. }
    20.  
    21. void MainWindow::createMenus()
    22. {
    23. fileMenu = new QMenu( tr("&File") );
    24. fileMenu->addAction(prefsAct);
    25. fileMenu->addSeparator();
    26. fileMenu->addAction( exitAct );
    27.  
    28. helpMenu = new QMenu( "&Help" );
    29. helpMenu->addAction( helpAct);
    30. helpMenu->addAction(aboutAct);
    31. helpMenu->addAction(QWhatsThis::createAction());
    32.  
    33. menuBar()->addMenu(fileMenu);
    34. menuBar()->addMenu(helpMenu);
    35. }
    To copy to clipboard, switch view to plain text mode 
    createActions and createMenus are both called from the constructor. If I comment out createMenus so that is not called anymore the Ctrl+P entry does not work anylonger. But when I remove the comments it works as expected.

    Any further ideas?

    P.s. I use Qt-4.2.0-rc1
    Last edited by momesana; 1st October 2006 at 14:56.

  4. #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: Shift+F1 only works if a QWhatsThis action has been added to the menu/toolbar

    You were right, it doesn't work. I made it work this way:

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QAction>
    4. #include <QKeySequence>
    5.  
    6. int main(int argc, char **argv){
    7. QApplication app(argc, argv);
    8. QAction act("quit", &w);
    9. act.connect(&act, SIGNAL(triggered()), &w, SLOT(close()));
    10. act.setShortcut(QKeySequence(Qt::Key_Escape));
    11. w.addAction(&act); // this here does the trick
    12. w.show();
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

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

    momesana (1st October 2006)

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.