QMenu questions for renaming and removing implementation

Hi all, I have 2 questions for my implementation towards QMenu

  1. (real-time) renaming

Using the following code, I have manage to get the adding of menu items working, but if I wanted to rename an already existed item in the context menu and not so sure on how to proceed with the above 2 that I would like to implement.

For the renaming of items, I am thinking of using the mouse double-clicking, in which I have implemented, see the code but I was stumped after the mouseReleaseEvent as I am not seeing any viable methods in QMenu docs that I can implement …

  1. Removing of menu item in QMenu via some method…
    I need some advice on how I should proceed with this?
    There is a method called removeAction but for any new menu items that I have created, it already has its own set of function to call to.
    How/ What is the best way that I can remove it - is there a close icon etc (something akin to the closebutton in tab widget), set another right click menu?

Appreciate in advance for any guidance.

This is my code:

class MyWin(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyWin, self).__init__()

        self.rename_menu_allowed = False

        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 mouseDoubleClickEvent(self, event):
        super(MyWin, self).mouseDoubleClickEvent(event)
        self.rename_menu_allowed = True

    def mouseReleaseEvent(self, event):
        super(MyWin, self).mouseReleaseEvent(event)
        if self.rename_menu_allowed:
            menu_index = self.my_extra_menus.actionAt(event.pos())
            # Unsure of how to proceed from here...

    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()