2

I have a custom widget that extends QMainWindow. There I am adding a number of QActions to the menu bar, along with keyboard shortcuts for each, and they work fine. Now I want to remove some of those actions from the menubar, but I want to keep the shortcuts enabled (the user can know about the availability of the shortcuts from a Help dialog). So first I decided that I will make the actions invisible.

That did not work, so I guess the action cannot be invisible if the shortcuts have to work. So I added it to the main window, still they are not working. Any idea, how do I make it work? Here is my code. Whatever needs to happen is there in the method someMethod.

class MyWidget: public QMainWindow {
    public:
        MyWidget();

};

MyWidget::MyWidget() {
    QAction *myAct = new QAction(tr("&Some Text"), this);
    fNextmyActPageAct->setShortcut(QKeySequence(Qt::Key_Right));
    myAct->setVisible(false); //adding this does not work
    connect(myAct, SIGNAL(triggered()), this, SLOT(someMethod()));

    ...

    QMenu *someMenu = menuBar()->addMenu(tr("&Some Menu"));
    someMenu->addAction(myAct); //this works, the option shows up in the menu 'Some Menu' and the shortcut works
    this->addAction(myAct); //does not work

}
Nejat
  • 31,784
  • 12
  • 106
  • 138
SexyBeast
  • 7,913
  • 28
  • 108
  • 196

2 Answers2

5

I tested this code and it's working fine :

QAction* myAct = new QAction(this);
myAct->setShortcut(Qt::Key_Right);
connect(myAct, SIGNAL(triggered()), this, SLOT(someMethod()));
this->addAction(myAct);

Do not add QAction to your menuBar.

Ali Mofrad
  • 308
  • 5
  • 21
  • 1
    Umm, no, it doesn't work. And I didn't expect it, you aren't doing anything different than I was. – SexyBeast Nov 22 '14 at 11:46
  • I tested this code and i am sure this way working. use this 4 line only in new project for test. – Ali Mofrad Nov 22 '14 at 11:54
  • I have used this. The only difference between your and my code is that you didn't add the text during initialization, and used the shortcut directly instead of wrapping it in `QkeySequence`. Neither is supposed to make any difference. Still I tried, but it doesn't work. – SexyBeast Nov 22 '14 at 12:06
  • No code isn't explicitly setting the focus on the widget, I try the shortcut after I have clicked on the widget. So clicking should naturally shift the focus on the widget, still not working. – SexyBeast Nov 22 '14 at 12:53
  • @Cupidvogel Sorry but i had to ask: Did you define someMethod() as public SLOT? – Ali Mofrad Nov 22 '14 at 16:56
0

You can use QShortcut and pass the key, the target widget and the relevant slot as parameters to it's constructor. Just put this in the constructor of MyWidget :

QShortcut * shortcut = new QShortcut(QKeySequence(Qt::Key_Right),this,SLOT(someMethod()));
shortcut->setAutoRepeat(false);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • This works for me. May be that's because you are also using the same key for the action. Try to change the key to see if it works or remove the shortcut key for the action. – Nejat Nov 22 '14 at 12:15
  • Nah, completely commented out the action part, still not working. I am cursed.. :( – SexyBeast Nov 22 '14 at 12:22
  • Your widget should be focused for the shortcut to take action. Does your widget have focus when you press the key? – Nejat Nov 22 '14 at 12:26
  • I haven't written any code to set the focus on the widget upon clicking explicitly. Do I need to do it? I thought clicking on the widget automatically sets the focus on the widget. How do I do it, then? – SexyBeast Nov 22 '14 at 12:29
  • If you click on the widget then it gets focus. – Nejat Nov 22 '14 at 12:39
  • Yeah, that I am doing all right, before I try out the shortcut. Still it doesn't work.. – SexyBeast Nov 22 '14 at 12:41