Assign shortcut keys to buttons - Qt C++

前端 未结 4 2001
野趣味
野趣味 2021-01-31 18:17

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

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-31 19:09

    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 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.

提交回复
热议问题