0

I need to have several shortcuts for one push button. For example Ctrl+W and Enter and Return (Enter and Return are different in Qt), any of them would cause a click on the button. How to do this? If the button was QAction, I would call setShortcuts() ( See Two shortcuts for one action which is NOT a duplicate. It is different, relates to QAction not QPushButton. ) but QPushButton has only setShortcut() (singular), which seems to not allow this. What solution or hack wold you suggest?

  • 1
    set shortcuts for QAction, conect action's trigerred signal to same slot you connect pushbutton's on_click signal. – Andrew Kashpur Apr 12 '18 at 10:32
  • 1
    A hack? OK, just redirect several shortcuts from their slots to whatever you need xD – Smit Ycyken Apr 12 '18 at 10:32
  • The big question is really: why would you do such a thing? – Clearer Apr 12 '18 at 10:34
  • 2
    @Clearer so that you can use the numpad enter or the central return button ... – UKMonkey Apr 12 '18 at 10:39
  • I could create actions to call the same slot as the button calls but this does not animate the click on the button. I tried connecting the actions to `animateClick()` slot of the button but it does not visualize the click either. – HiFile.app - best file manager Apr 12 '18 at 10:41
  • 2
    Possible duplicate of [Two shortcuts for one action](https://stackoverflow.com/questions/23388754/two-shortcuts-for-one-action) – Clearer Apr 12 '18 at 10:42
  • @UKMonkey - thank you - `Return` and `Enter`- this was exactly the main reason why I came up with this question. This is a bit annoying here, whenever I ask a question, there is always someone asking "Why do you need this?" Well, I need it, otherwise I would not ask. :) – HiFile.app - best file manager Apr 12 '18 at 10:44
  • @Clearer I have already linked this in my original question. And I explained why this does not work in my case. – HiFile.app - best file manager Apr 12 '18 at 10:45
  • 1
    @V.K. don't forget though half the questions here are XY problems - they're asking to help you better; in addition if it's a good reason, then they learn something ... win win! :) – UKMonkey Apr 12 '18 at 10:46
  • XY problems are usually more complicated stuff. I believe this one is very simple. As the last resort I will override the `QPushButton`, intercept `keyPressEvent` and emit some click event to the button to visualize it. But I believe this overkill for such a simple thing. I hope there is simpler solution. :) – HiFile.app - best file manager Apr 12 '18 at 10:48
  • @V.K. you could always catch the ctrl+w and then mark the button as selected - then either enter button, or space will trigger it – UKMonkey Apr 12 '18 at 10:53
  • @V.K., when people ask about hacks and extraordinary methods, mostly their question is XY problem. – Smit Ycyken Apr 12 '18 at 11:00

1 Answers1

0

OK, I have got a solution which is not that hacky. I can create a QPushButton and a QAction, then set multiple shortcuts for QAction using QAction::setShortcuts() and connect this action to QPushButton::animateClick(). At first this solution did not work because I called connect(action, &QAction::triggered, button, &QPushButton::animateClick);. The problem was with invisible default arguments. QAction::triggered sends true/false indicating whether the action is checked. But QPushButton::animatedClick expects number of milliseconds to remain visually 'pressed'. Therefore it remained 'pressed' only for 0 or 1 millisecond, which is not enough to notice it (the default value of the argument is 100 milliseconds). This can be solved using lambda, hence:

// 'this' refers to a parent widget
auto action = new QAction(this);
action->setShortcuts({ tr("Ctrl+W"), tr("Return"), tr("Enter") });
this->addAction(action);
auto button = new QPushButton(tr("Hey, click me!"));
connect(action, &QAction::triggered, [button](){ button->animateClick(); });

And that's it.