I am working on a Python project that uses Qt Designer to build interface. when working on building a plugin capability, I was able to allow dynamic loading of user plugins and create a new QMenu
item to add to the main menubar. The problem is that there seems to be no way of removing that top level QMenu
once it is added to the main menubar. I researched/searched quite a bit on this topic and it seems that every solution related to this topic is for removing sub-menu items from a QMenu
via removing its actions, not for removing that dynamically-added QMenu
itself. I hope someone would point out this to be a simple thing, and provide a code snippet to demo how this is done.
Asked
Active
Viewed 2,453 times
2

user5688856
- 23
- 5
-
Are you using QMainWindow ? – Achayan Dec 17 '15 at 01:22
-
yes, my form is sub to QMainWindow – user5688856 Dec 17 '15 at 02:22
-
self.setWindowFlags(Qt.Dialog) & self.setWindowModality(Qt.ApplicationModal) don't do what you looking for ? – Achayan Dec 17 '15 at 02:56
-
I am not sure about the relevance of your suggestion to my original problem that is to remove a dynamically added QMenu on the main menu bar of a QMainwindow form – user5688856 Dec 17 '15 at 03:55
-
Oh sorry I read wrong, I thought you want to remove QMenuBar – Achayan Dec 17 '15 at 04:33
2 Answers
2
Achayan's solution above crashes on python2 qt4 (Windows) for the deletion
Better way for it is to to use the clear function.
Adding to the solution above,
def removeMenu():
self.main_menu.clear()

eyllanesc
- 235,170
- 19
- 170
- 241

Arvind Sreekumar
- 31
- 4
0
Hope this will give you idea for what you upto. And I took some part from another post, which is same qmenu thing
import sys
# This is bad, but Iam lazy
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.main_menu = self.menuBar()
widget = QWidget()
self.menuList = []
layout2 = QVBoxLayout(widget)
self.menuButton = QPushButton("Add Menu")
self.menuRmButton = QPushButton("Remove Menu")
layout2.addWidget(self.menuButton)
layout2.addWidget(self.menuRmButton)
self.menuButton.clicked.connect(self.create_menu)
self.menuRmButton.clicked.connect(self.removeMenu)
self.setCentralWidget(widget)
def create_menu(self):
menu2 = self.main_menu.addMenu('Menu 1')
self.menuList.append(menu2)
Action1=QAction('Menu 1 0',self)
Action1.triggered.connect(self.action_1)
menu2.addAction(Action1)
Action2=QAction('Menu 1 1',self)
Action2.triggered.connect(self.action_2)
menu2.addAction(Action2)
def removeMenu(self):
if self.menuList:
for eachMenu in self.menuList:
menuAct = eachMenu.menuAction()
self.main_menu.removeAction(menuAct)
# just for safe side
menuAct.deleteLater()
eachMenu.deleteLater()
def action_1(self):
print('Menu 1 0')
def action_2(self):
print('Menu 1 1')
if __name__ == '__main__':
app=QApplication(sys.argv)
new=MyWindow()
new.show()
app.exec_()

Achayan
- 5,720
- 2
- 39
- 61
-
Achayan, this worked. I think I tried this approach myself by attaching an action to the top level QMenu, but perhaps my syntax was not quite right. Especially the Action1 = QAction('', self) statement and the use of self as the 2nd argument. I think to get rid of the "additional" fake sub-menu item (i.e. 'Menu 1 0'), one need to set Action1 to invisible like Action1.setVisible(False) – user5688856 Dec 17 '15 at 05:23