作为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)
下面是导致按钮:http://i.stack.imgur.com/tb5Mh.png(作为一个小白,我不允许直接嵌入图片进入后)。
我希望这是有帮助的。