Page 1 of 1

use space bar as shortcut

Posted: Mon Sep 14, 2020 9:17 am
by fblue
Hello,
In my plugin, I'd like to use the space bar to invoke an action (ex: change the state of a toolButton in plugin). Is it possible to do that? and what if I use other shortcuts as "CTRL or ALT"?

Thank you in advance!

Re: use space bar as shortcut

Posted: Tue Sep 15, 2020 12:58 pm
by daniel
I believe you 'just' need to use the QShortcut object?

Re: use space bar as shortcut

Posted: Wed Sep 16, 2020 1:23 pm
by fblue
Yes indeed.
But I've tried to set up QShortcut in my plugin by many ways, but nothing happens to the shortcut key.
The codes are like this:
QShortcut shortcut_pause = new QShortcut(QKeySequence(Qt::Key_F2), this);
connect(shortcut_pause, SIGNAL(activated()), this, SLOT(onShortcutTriggered()));

Then I found another post: http://www.cloudcompare.org/forum/viewt ... cut#p20715

So, as you said in the post, it's not possible to define custom shortcuts, in 'mainwindow' I suppose? Is it the reason why QShortcut didn't work in my plugin? or maybe there is something wrong somewhere in my program?

To precise: I've placed a break point in the beginning of the slot. But the program never goes into the slot when I press the key. I've also tried other keys, same problem.

Re: use space bar as shortcut

Posted: Wed Sep 16, 2020 4:45 pm
by daniel
I believe we were referring to user-defined shortcuts in this post.

In your case, I believe it's more a problem of context/focus. Do you add your shortcut to a dialog? Or is the user interacting with the 3D view? (because in this case, it's the 3D view that will process the shortcuts). You normally have to install a custom event filter if you want to catch the shortcuts (or other events).