<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Creating and storing different QMenus for different tabs]]></title><description><![CDATA[<p dir="auto">Hi all, I am trying to achieve a right-mouse click menu where if User choose to add in new items, these new items will be populated right under the ‘Add New Item’…</p>
<p dir="auto">The design is something as follows:</p>
<pre><code>|  Add New Item   |
-------------------
|   New Item 01   |
|   New Item 02   |
-------------------
| Default Item 01 |
</code></pre>
<p dir="auto">If the QMenu is ‘new’, by default it will looks somethings like this:</p>
<pre><code>|  Add New Item   |
-------------------
| Default Item 01 |
</code></pre>
<p dir="auto">In my current code, as I am integrating this with the tabs, I am having issues where:</p>
<ul>
<li>unable to store the different new created items per tab, currently all tabs are using the same QMenu</li>
<li>default items are getting populated more than once per right-mouse click</li>
</ul>
<p dir="auto">This is my code:</p>
<pre><code>class TabBar(QtGui.QTabBar):
    def __init__(self, parent=None):
        super(TabBar, self).__init__(parent)

        self.qmenu = QtGui.QMenu()
        self.setExpanding(False)

        add_item_action = QtGui.QAction('Add new item', self,
            triggered=self.add_new_item)
        self.qmenu.addAction(add_item_action)
        self.qmenu.addSeparator()

        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
        def_item_01 = self.qmenu.addAction("Default Item A")
        def_item_02 = self.qmenu.addAction("Default Item B")

        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 = QtGui.QInputDialog.getText(
            self,
            "name of item",
            "Name of new item : "
        )
        if ok:
            new_action = QtGui.QAction(new_item_name, self.qmenu, checkable=True)
            self.qmenu.insertAction(self.separator, new_action)


class TabWidget(QtGui.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(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.tabs = TabWidget(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.tabs)

        some_tabs = ['TabA', 'TabB', 'TabC', 'TabD']
        for my_tab in some_tabs:
            self.tabs.addTab(QtGui.QWidget(self), my_tab)
</code></pre>
<p dir="auto">Many thanks in advance for any replies.</p>
]]></description><link>https://forum.qt.io/topic/98429/creating-and-storing-different-qmenus-for-different-tabs</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 04:31:56 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/98429.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 11 Jan 2019 18:33:06 GMT</pubDate><ttl>60</ttl></channel></rss>