作爲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(作爲一個小白,我不允許直接嵌入圖片進入後)。
我希望這是有幫助的。