我正在尝试使用QComboBox 来获得类似 QComboBox的行为。Namely, when an action is selected, I want to connect to a slot, and for the slot to print out the index of the selected item. 相关类定义:QPushButton
QMenu
from PySide import QtGui, QtCore
class PushIt(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
menBut=self.makeQMenuButton()
self.show()
def makeQMenuButton(self):
button=QtGui.QPushButton("Men...you!", self)
pushMenu=QtGui.QMenu(button)
#Set up each element of menu
menBut1=pushMenu.addAction("Select me!")
menBut2=pushMenu.addAction("Don't select me!")
#What to do when each one is activated
menBut1.activated[int].connect(self.menButSlot)
menBut2.activated[int].connect(self.menButSlot)
button.setMenu(pushMenu)
return button
@QtCore.Slot(int)
def menButSlot(self, menDat):
print "Menu button ", menDat
它有点工作。例如,当我按下按钮 1 时,它会返回:
Menu button -3
这与我使用组合框得到的结果接近,除了计数似乎从 -3 而不是 0 开始。我认为它会从 0 开始,所以我认为我根本没有访问索引,而是在玩弄我不明白的权力。
我在 Windows 7 的 iPython/Anaconda 中使用 PySide。