PDA

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



momesana
1st October 2006, 14:52
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

wysota
1st October 2006, 15:23
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 ).

momesana
1st October 2006, 15:44
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 ...



void MainWindow::createActions()
{
prefsAct = new QAction( tr("&Preferences"), this );
prefsAct->setShortcut( tr("Ctrl+P") );
prefsAct->setShortcutContext( Qt::ApplicationShortcut);
connect( prefsAct, SIGNAL( activated() ), this, SLOT( setPreferences() ) );

exitAct = new QAction( QIcon(":images/exit.png"), tr("&Exit"), this );
exitAct->setToolTip( tr("Exit the application") );
connect(exitAct, SIGNAL(activated()), qApp, SLOT(quit()));

helpAct = new QAction ( QIcon(":images/help.png"), tr("Online Help"), this);
helpAct->setToolTip( tr("Online Help") );
connect( helpAct, SIGNAL(activated()), this, SLOT(help()));

aboutAct = new QAction ( tr("About servant"), this);
aboutAct->setToolTip( tr("About servant") );
connect( aboutAct, SIGNAL(activated()), this, SLOT(about()));
}

void MainWindow::createMenus()
{
fileMenu = new QMenu( tr("&File") );
fileMenu->addAction(prefsAct);
fileMenu->addSeparator();
fileMenu->addAction( exitAct );

helpMenu = new QMenu( "&Help" );
helpMenu->addAction( helpAct);
helpMenu->addAction(aboutAct);
helpMenu->addAction(QWhatsThis::createAction());

menuBar()->addMenu(fileMenu);
menuBar()->addMenu(helpMenu);
}

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

wysota
1st October 2006, 16:03
You were right, it doesn't work. I made it work this way:


#include <QApplication>
#include <QWidget>
#include <QAction>
#include <QKeySequence>

int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget w;
QAction act("quit", &w);
act.connect(&act, SIGNAL(triggered()), &w, SLOT(close()));
act.setShortcut(QKeySequence(Qt::Key_Escape));
w.addAction(&act); // this here does the trick
w.show();
return app.exec();
}