Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Program custom Keyboard Shortcuts

I have a Qt application on Linux.

I'd like to program custom keyboard shortcuts such as CTRL-Q which will then call a subroutine which quits the program.

How can I do this?

like image 639
JB_User Avatar asked Jul 13 '13 15:07

JB_User


1 Answers

Try this:

new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close())); 

You can create it in the contructor of your form. This allows to avoid polluting your class with a pointer to access the shortcut. You may still want to add a pointer to the shortcut if you want to access it later on. The shortcut will be deleted when the application exits, since it is parented to it. It automatically does the connection, you don't have to do it manually.

Also note that there is no default Ctrl+Q sequence on Windows, but there is one on Linux and MacOS.

like image 160
dtech Avatar answered Oct 09 '22 00:10

dtech