C++ 设置快捷键的多个键盘快捷键

C++ 设置快捷键的多个键盘快捷键,c++,qt,C++,Qt,我想知道是否有人知道如何为一个按钮设置多个快捷方式。例如,我有一个QPushButton,我想链接到回车键和回车键(键盘和数字键盘) 如果我在designer中输入快捷方式字段: Return, Enter 只有回车键响应,而不是回车键 我还尝试在designer和我的源代码中设置返回: ui.searchButton->setShortcut( tr("Enter") ); 这似乎也只响应输入(数字键盘)而不响应返回(键盘) 有人知道如何设置QPushButton的多个快捷方式吗?仅

我想知道是否有人知道如何为一个按钮设置多个快捷方式。例如,我有一个QPushButton,我想链接到回车键和回车键(键盘和数字键盘)

如果我在designer中输入快捷方式字段:

Return, Enter
只有回车键响应,而不是回车键

我还尝试在designer和我的源代码中设置返回:

ui.searchButton->setShortcut( tr("Enter") );
这似乎也只响应输入(数字键盘)而不响应返回(键盘)


有人知道如何设置QPushButton的多个快捷方式吗?仅供参考,我使用的是Qt4.7。

我不使用QtCreator,因此这里有两个解决此问题的代码解决方案


1.
对于这些情况,我会覆盖
按键事件
(例如,主窗口或您希望快捷方式出现的位置)

标题:

protected:
    virtual void keyPressEvent( QKeyEvent* e );
资料来源:

void main_window::keyPressEvent( QKeyEvent* e )
{
    switch( e->key() )
    {
    case Qt::Key_Enter:
    case Qt::Key_Return:
        // do what you want, for example:
        QMessageBox::information( this,
            "Success",
            "Let me guess, you pressed the return key or the enter key." );
        break;
    default:
        ;
    }

    QMainWindow::keyPressEvent( e );
}

2.
我认为也可以创建和连接多个项目。
只需创建所需的所有快捷方式,并将其
激活的
-信号连接到要接收快捷方式的对象的插槽。

我不使用QtCreator,因此这里有两个解决此问题的代码解决方案


1.
对于这些情况,我会覆盖
按键事件
(例如,主窗口或您希望快捷方式出现的位置)

标题:

protected:
    virtual void keyPressEvent( QKeyEvent* e );
资料来源:

void main_window::keyPressEvent( QKeyEvent* e )
{
    switch( e->key() )
    {
    case Qt::Key_Enter:
    case Qt::Key_Return:
        // do what you want, for example:
        QMessageBox::information( this,
            "Success",
            "Let me guess, you pressed the return key or the enter key." );
        break;
    default:
        ;
    }

    QMainWindow::keyPressEvent( e );
}

2.
我认为也可以创建和连接多个项目。
只需创建所需的所有快捷方式,并将其激活的
-信号连接到要接收快捷方式的对象插槽。

似乎是一个小小的解决办法,但您可以使用QAction set multiple并将其连接到QPushButton。(类似地,您可以创建多个QShortcut对象并将其连接到按钮。)

似乎是一个小解决方案,但您可以使用QAction集multiple并将其连接到QPushButton。(类似地,您可以创建多个QShortcut对象并将它们连接到按钮。)

作为一个qt noob,我正在寻找一种向一个按钮添加多个快捷方式的方法。这里的答案很有帮助,但我仍然需要努力才能真正把所有的部分放在一起。所以我想我会在这里发布完整的答案,希望能帮助其他来找我的人

很抱歉,这是用PyQt写的,但我相信它会传达这个想法

# Create and setup a "Find Next" button
find_next_btn = QtGui.QPushButton("      Find &Next")
# setupButton is a small custom method to streamline setting up many buttons. See below.
setupButton(find_next_btn, 150, "Icons/arrow_right_cr.png", 30, 20, "RTL")
find_next_btn.setToolTip("Search DOWN the tree")
find_next_btn.clicked.connect(find_next)
# find_next is the method executed when the button is pressed

# Create an action for the additional shortcuts. Alt+N is already set
# by "&" in "Find &Next"
find_next_ret_act = QtGui.QAction(self, triggered=find_next_btn.animateClick)
find_next_ret_act.setShortcut(QtGui.QKeySequence("Return"))

find_next_enter_act = QtGui.QAction(self, triggered=find_next_btn.animateClick)
find_next_enter_act.setShortcut(QtGui.QKeySequence("Enter"))

# Now add (connect) these actions to the push button
find_next_btn.addActions([find_next_ret_act, find_next_enter_act])


# A method to streamline setting up multiple buttons
def setupButton(button, btn_w, image=None, icon_w=None, icon_h=None, layout_dir=None):
    button.setFixedWidth(btn_w)
    if image != None:            
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(image))
        button.setIcon(icon)
    if icon_w != None:
        button.setIconSize(QtCore.QSize(icon_w, icon_h))
    if layout_dir == "RTL":
        find_next_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
下面是结果按钮:(作为noob,我不允许将图片直接嵌入帖子。)


我希望这会有帮助。

作为一个qt noob,我正在寻找一种向一个按钮添加多个快捷方式的方法。这里的答案很有帮助,但我仍然需要努力才能真正把所有的部分放在一起。所以我想我会在这里发布完整的答案,希望能帮助其他来找我的人

很抱歉,这是用PyQt写的,但我相信它会传达这个想法

# Create and setup a "Find Next" button
find_next_btn = QtGui.QPushButton("      Find &Next")
# setupButton is a small custom method to streamline setting up many buttons. See below.
setupButton(find_next_btn, 150, "Icons/arrow_right_cr.png", 30, 20, "RTL")
find_next_btn.setToolTip("Search DOWN the tree")
find_next_btn.clicked.connect(find_next)
# find_next is the method executed when the button is pressed

# Create an action for the additional shortcuts. Alt+N is already set
# by "&" in "Find &Next"
find_next_ret_act = QtGui.QAction(self, triggered=find_next_btn.animateClick)
find_next_ret_act.setShortcut(QtGui.QKeySequence("Return"))

find_next_enter_act = QtGui.QAction(self, triggered=find_next_btn.animateClick)
find_next_enter_act.setShortcut(QtGui.QKeySequence("Enter"))

# Now add (connect) these actions to the push button
find_next_btn.addActions([find_next_ret_act, find_next_enter_act])


# A method to streamline setting up multiple buttons
def setupButton(button, btn_w, image=None, icon_w=None, icon_h=None, layout_dir=None):
    button.setFixedWidth(btn_w)
    if image != None:            
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(image))
        button.setIcon(icon)
    if icon_w != None:
        button.setIconSize(QtCore.QSize(icon_w, icon_h))
    if layout_dir == "RTL":
        find_next_btn.setLayoutDirection(QtCore.Qt.RightToLeft)
下面是结果按钮:(作为noob,我不允许将图片直接嵌入帖子。)

我希望这是有帮助的