为按钮分配快捷键 - Qt C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4629629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assign shortcut keys to buttons - Qt C++
提问by CrazyCoder
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.
我使用 Qt Creator 创建了一个 GUI。那是通过拖放小部件。现在我想为所有按钮分配快捷键。这里有人可以让我知道怎么做吗?先感谢您。
回答by houbysoft
Your buttons probably have a slot connected to their clicked()
signal.
您的按钮可能有一个插槽连接到它们的clicked()
信号。
To add shortcut keys, you simply connect a shortcut key's activated()
signal to the same slot.
要添加快捷键,您只需将快捷键的activated()
信号连接到同一个插槽。
In your code, #include <QShortcut>
and then you will be able to add a shortcut key for a slot like this:
在您的代码中,#include <QShortcut>
然后您将能够为这样的插槽添加快捷键:
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.
parent 是您的快捷方式的父级(例如主窗口),yourSlotHere()
是您希望快捷方式调用的插槽的名称,以及receiver
对象所在的yourSlotHere()
位置。
Replace "Ctrl+O"
with whatever shortcut you want to assign.
替换"Ctrl+O"
为您要分配的任何快捷方式。
You can also find more information on the documentation page for QShortcut.
您还可以在QShortcut的文档页面上找到更多信息。
回答by Pavel Bazant
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 &&.
或者,如果快捷键对应于按钮文本中的某个字符,您可以在该字符前加上 &。如果您想要文字 &,请使用 &&。
回答by Plouff
Today (Qt5.7), we can assign shortcuts directly in Qt Designer using the shortcutproperty:
今天(Qt5.7),我们可以直接在Qt Designer中使用shortcut属性指定快捷方式:
Pretty handy.. Even if a bit buggy: I have to "validate" the shortcut by clicking on another property of the same widget before switching to another widget!
非常方便......即使有点错误:在切换到另一个小部件之前,我必须通过单击同一小部件的另一个属性来“验证”快捷方式!
But it works.
但它有效。
回答by V.K.
From a good UI/UX perspective, what you actually what you want is not just to trigger the same slot as the button triggers (which is the solution suggested by the accepted answer) but you also want to visually animate the button being pressed to make sure the user clearly can visually notice the action being triggered. The following is what I use for example for my 'confirm' QPushButtons
.
从良好的 UI/UX 角度来看,您实际上想要的不仅仅是触发与按钮触发器相同的插槽(这是已接受的答案建议的解决方案),而且您还希望在视觉上为按下的按钮设置动画确保用户可以清楚地看到被触发的动作。以下是我用于 'confirm' 的示例QPushButtons
。
// I have this function in my 'utils' module.
void bindShortcut(QAbstractButton *button, const QKeySequence &shortcut)
{
QObject::connect(new QShortcut(shortcut, button), &QShortcut::activated, [button](){ button->animateClick(); });
}
// and then I use it like this
auto *confirmButton = new QPushButton(tr("Confirm"));
connect(confirmButton, &QPushButton::clicked, ... some slot of yours to do the actual work);
bindShortcut(confirmButton, Qt::Key_Enter);
bindShortcut(confirmButton, Qt::Key_Return);
This is I think the best answer if you are not using QtDesigner. Otherwise you can set the shortcuts in the designer easily as another answer suggests.
如果您不使用 QtDesigner,这是我认为最好的答案。否则,您可以按照另一个答案的建议轻松地在设计器中设置快捷方式。