Hey likage: I cannot repro your second question. Items are added properly into the QMenu on my site. For your second question, because you put the "addAction" (2 of the default items) under the "_showContextMenu" , so theyare added when you right clicking the tab. To be noticed, I am using Pyside2, so I placed all the QtGui to QtWidgets.
I paste the new code, hope that helps: from PySide2 import QtWidgets from PySide2 import QtCore class TabBar(QtWidgets.QTabBar ): def __init__(self, parent=None): super(TabBar, self).__init__(parent) self.qmenu = QtWidgets.QMenu() self.setExpanding(False) add_item_action = QtWidgets.QAction('Add new item', self, triggered=self.add_new_item) self.qmenu.addAction(add_item_action) self.qmenu.addSeparator() def_item_01 = self.qmenu.addAction("Default Item A") def_item_02 = self.qmenu.addAction("Default Item B") self.separator = self.qmenu.addSeparator() def tabSizeHint(self, index): return super(TabBar, self).tabSizeHint(index) def mousePressEvent(self, event): index = self.tabAt(event.pos()) if event.button() == QtCore.Qt.RightButton: self._showContextMenu(event.pos(), index) else: super(TabBar, self).mousePressEvent(event) def _showContextMenu(self, position, index): # Default items 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() )) @QtCore.Slot() def add_new_item(self): new_item_name, ok = QtWidgets.QInputDialog.getText( self, "name of item", "Name of new item : " ) if ok: new_action = QtWidgets.QAction(new_item_name, self.qmenu, checkable=True) self.qmenu.insertAction(self.separator, new_action) class TabWidget(QtWidgets.QTabWidget): def __init__(self, parent=None): super(TabWidget, self).__init__(parent) self.setTabBar(TabBar(self)) def resizeEvent(self, event): self.tabBar().setMinimumWidth(self.width()) super(TabWidget, self).resizeEvent(event) class Window(QtWidgets.QWidget): def __init__(self): super(Window, self).__init__() self.tabs = TabWidget(self) layout = QtWidgets.QVBoxLayout(self) layout.addWidget(self.tabs) some_tabs = ['TabA', 'TabB', 'TabC', 'TabD'] for my_tab in some_tabs: self.tabs.addTab(QtWidgets.QWidget(self), my_tab) test = Window() test.show() Tenghao Wang Sr. Technical Artist Visual Concepts On Mon, Jan 14, 2019 at 4:06 PM likage <dissidia....@gmail.com> wrote: > Hi everyone, I have a tool which utilizes QTabWidget and the tabs are > populated with a list of keys from within a dictionary. > Each of the QMenu are equipped with 3 default options, with one of the > option called `Add new item` where when User uses that option, a prompt > dialog will popped up and adds in the user-inputted naming as a new menu > option into the current QMenu of the tab (where the right mouse click > happens on) > > The problems that I am encountering right now is: > 1. 2 of the default items in the QMenu are getting added more than once, > depending on the number of right mouse clicks made on the tool within the > session > 2. no new item are added into the QMenu - after I added in a new name for > the prompt > when performing another right-click, the menu is still showing > the default items plus 2 additional default items as mentioned in #1 > > Even so, wondering if this is a possible scenario for tabs + qmenu(s)? > I asked this because when doing a google search online, it seems that > QMenu is generally used as a generic menu (eg. multiple buttons adopts the > same menu etc) > > Or should I be adopting to use a different widget for my case? > > This is my code - https://pastebin.com/raw/4nakpnm8 > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to python_inside_maya+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/b9cccfda-a8c5-4f00-bcd2-689ea4777640%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/b9cccfda-a8c5-4f00-bcd2-689ea4777640%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAKoVHSCOpHNfGpwNxHhrkq3PuG2Z3wm5VnvBh3D4nT2kQsL4FA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.