我对QMenu的撕下存在问题,除非我关闭撕下并重新撕下,否则它不会使用我创建的新项目进行更新。
在我的以下示例中,如果您这样做:
Add menu item
soda
,则无法看到新选项。但是,如果我再次单击鼠标右键,请单击“饮料”选项卡,该选项就在那里。但是,如果我对_addActionItem
中的菜单执行相同的操作,则可以立即在撕下框中看到我的新选项。
有什么想法吗?
class MyWin(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWin, self).__init__()
central_widget = QtGui.QWidget()
self.setCentralWidget(central_widget)
vlay = QtGui.QVBoxLayout(central_widget)
hlay = QtGui.QHBoxLayout()
vlay.addLayout(hlay)
vlay.addStretch()
self.add_button = QtGui.QToolButton()
self.tab_bar = QtGui.QTabBar(self)
self.add_button.setIcon(QtGui.QIcon('add.png'))
self.add_button.setMenu(self.set_menu())
self.add_button.setPopupMode(QtGui.QToolButton.InstantPopup)
self.tab_bar.setTabButton(
0,
QtGui.QTabBar.ButtonPosition.RightSide,
self.add_button
)
hlay.addWidget(self.add_button)
hlay.addWidget(self.tab_bar)
self.my_extra_menus = defaultdict(list)
def set_menu(self):
menu_options = ['food', 'drinks', 'snacks']
qmenu = QtGui.QMenu(self.add_button)
for opt in menu_options:
qmenu.addAction(opt, partial(self.set_new_tab, opt))
qmenu.addAction
return qmenu
def set_new_tab(self, opt):
self.tab_bar.addTab(opt)
def mousePressEvent(self, event):
index = self.tab_bar.tabAt(event.pos())
if event.button() == QtCore.Qt.RightButton:
self._showContextMenu(event.pos(), index)
else:
super(MyWin, self).mousePressEvent(event)
def _showContextMenu(self, position, index):
self.qmenu = QtGui.QMenu(self)
self.qmenu.setTearOffEnabled(True) # New item is not shown in tearoff mode
self.qmenu.setTitle(self.tab_bar.tabText(index))
add_item_action = QtGui.QAction('Add Menu Item', self)
slot = functools.partial(self._addMenuItem, index)
add_item_action.triggered.connect(slot)
self.qmenu.addAction(add_item_action)
self.qmenu.addSeparator()
if self.my_extra_menus.get(index):
for menuItem in self.my_extra_menus[index]:
self.qmenu.addMenu(menuItem)
self.qmenu.addSeparator()
global_position = self.mapToGlobal(self.pos())
self.qmenu.exec_(QtCore.QPoint(
global_position.x() - self.pos().x() + position.x(),
global_position.y() - self.pos().y() + position.y()
))
def _addMenuItem(self, index):
first_tier_menu = []
for i in self.qmenu.actions():
first_tier_menu.append(i.text())
new_menu_name, ok = QtGui.QInputDialog.getText(
self,
"Name of Menu",
"Name of new Menu Item:"
)
if ok:
if new_menu_name in list(filter(None, first_tier_menu)):
self.err_popup()
else:
menu = QtGui.QMenu(new_menu_name, self)
menu.setTearOffEnabled(True) # New item is shown in tearoff mode, unless I close and re-tearoff
add_item_action = QtGui.QAction('Add sub Item', menu)
slot = functools.partial(self._addActionItem, menu)
add_item_action.triggered.connect(slot)
menu.addAction(add_item_action)
menu.addSeparator()
self.my_extra_menus[index].append(menu)
def _addActionItem(self, menu):
new_item_name, ok = QtGui.QInputDialog.getText(
self,
"Name of Menu Item",
"Name of new Menu Item:"
)
second_tier_menu = []
for i in menu.actions():
second_tier_menu.append(i.text())
if ok:
if new_item_name in list(filter(None, second_tier_menu)):
self.err_popup()
else:
action = QtGui.QAction(new_item_name, self)
slot = functools.partial(self._callActionItem, new_item_name)
action.setCheckable(True)
action.toggled.connect(slot)
menu.addAction(action)
def _callActionItem(self, name, flag):
print name
print flag
def err_popup(self):
msg = QtGui.QMessageBox()
msg.setIcon(QtGui.QMessageBox.Critical)
msg.setText("Input name already exists. Please check.")
msg.setWindowTitle('Unable to add item')
msg.setStandardButtons(QtGui.QMessageBox.Ok)
msg.exec_()
win = MyWin()
win.show()