我在PyQt中有一个简单的菜单代码,但问题是它没有显示子菜单。我希望在“更多”上方悬停时显示它。有什么问题?
class Menu(QtGui.QMenu):
def __init__(self, parent=None):
QtGui.QMenu.__init__(self, "Menu", parent)
sub_menu = QtGui.QMenu("Sub menu")
icon = QtGui.QIcon.fromTheme("edit-paste")
item = QtGui.QAction(icon, "More", self)
self.addAction(item)
icon = QtGui.QIcon.fromTheme("edit-copy")
item = QtGui.QAction(icon, "Submenu item 1", self)
item.triggered.connect(self.function)
sub_menu.addAction(item)
icon = QtGui.QIcon.fromTheme("edit-copy")
item = QtGui.QAction(icon, "Submenu item 2", self)
item.triggered.connect(self.function)
sub_menu.addAction(item)
self.addMenu(sub_menu)
icon = QtGui.QIcon.fromTheme("close")
item = QtGui.QAction(icon, "&Exit", self)
item.triggered.connect(self.quit_f)
self.addAction(item)
def function(self):
QtGui.QMessageBox.about(self.parent(), "About", "Test")
def quit_f(self):
sys.exit(0)
答案 0 :(得分:0)
您必须在创建子菜单时提供父级:
sub_menu = QtGui.QMenu("Sub menu", parent=self)
...
self.addMenu(sub_menu)
注意:这是创建子菜单的另一种更简洁的方法:
sub_menu = self.addMenu("Sub menu")