Reputation: 5067
I was wondering if anyone knows how to set up multiple shortcuts for one button. For example I have a QPushButton that I want to be linked to the Return key and the Enter key (keyboard and number pad).
If in designer I put in the shortcut field:
Return, Enter
only the Enter responds, not the Return.
I have also tried to just set the Return in designer and in my source code I put in:
ui.searchButton->setShortcut( tr("Enter") );
This also seems to only respond to Enter (number pad) not Return (keyboard).
Does anyone know how to set more than one shortcut to a QPushButton? FYI I'm using Qt4.7.
Upvotes: 1
Views: 3262
Reputation: 85
As a qt noob, I was searching for a way to add multiple shortcuts to one button. The answers here were helpful, but I still had to struggle a bit to actually put all of the pieces together. So I thought I would post the full answer here to, hopefully, help other noobs who come after me.
I apologize that this is written in PyQt, but I believe it will convey the idea.
# 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)
Here is the resulting button: https://i.sstatic.net/tb5Mh.png (as a noob, I am not allowed to embed pictures directly into the post.)
I hope this is helpful.
Upvotes: 3
Reputation: 25726
Seems to be a little workaround but you could use a QAction set multiple shortcuts on it and connect it to your QPushButton. (Similar you could create multiple QShortcut objects and connect them to the button.)
Upvotes: 1
Reputation: 4110
I don't work with QtCreator so here are 2 code solutions I would have for this problem.
1.
For those cases I overwrite the keyPressEvent
(e.g. of your main window or where you want the shortcut to be).
Header:
protected:
virtual void keyPressEvent( QKeyEvent* e );
Source:
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.
I think it is also possible to create and connect multiple QShortcut ojects.
Just create all the shortcuts you need and connect their activated
-Signal to a slot of the object which you want to receive the shortcut.
Upvotes: 1