Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign shortcut keys to buttons - Qt C++

Tags:

I have created a GUI using Qt Creator. That is by drag and drop the widgets. Now I want to assign shortcut keys for all of the buttons. Can anyone here please let me know how to do that? Thank you in advance.

like image 354
CrazyCoder Avatar asked Jan 07 '11 19:01

CrazyCoder


People also ask

How do I assign a shortcut to a button?

Right-click the control for which you want to set a keyboard shortcut, and then click Control Properties on the shortcut menu. Click the Advanced tab. In the Access key box, type a character. An access key is simply a keyboard shortcut that uses the ALT key as part of the shortcut.

How do I reassign hot keys?

To reassign a keyConnect the keyboard that you want to configure. Select the Start button, and then select Microsoft Mouse and Keyboard Center. From the displayed list of key names, select the key that you want to reassign. In the command list of the key that you want to reassign, select a command.


2 Answers

Your buttons probably have a slot connected to their clicked() signal.

To add shortcut keys, you simply connect a shortcut key's activated() signal to the same slot.

In your code, #include <QShortcut> and then you will be able to add a shortcut key for a slot like this:

QShortcut *shortcut = new QShortcut(QKeySequence("Ctrl+O"), parent); QObject::connect(shortcut, SIGNAL(activated()), receiver, SLOT(yourSlotHere())); 

Where parent is the parent of your shortcut (for example the main window), yourSlotHere() is the name of the slot you want the shortcut to call, and receiver the object where yourSlotHere() is.

Replace "Ctrl+O" with whatever shortcut you want to assign.

You can also find more information on the documentation page for QShortcut.

like image 172
houbysoft Avatar answered Oct 11 '22 03:10

houbysoft


Alternatively, if the shortcut key corresponds to some character in the text of the button, you can preped & to that character. If you want a literal &, use &&.

like image 42
Pavel Bazant Avatar answered Oct 11 '22 04:10

Pavel Bazant