JokerMartini
JokerMartini

Reputation: 6147

set all QAction items enabled to False within tool?

Is there a way in pyside to set all QAction items wherever they may be within the tool to be enabled = False?

For example say i have this written in my code...

# context menu
        self.edit_menu_factions = QtGui.QMenu()
        self.renameFaction = self.edit_menu_factions.addAction('Rename')
        self.renameFaction.triggered.connect(self.menu_action)
        self.removeFaction = self.edit_menu_factions.addAction('Remove')
        self.removeFaction.triggered.connect(self.menu_action)
        self.edit_menu_factions.addSeparator()
        self.copyFactionNodes = self.edit_menu_factions.addAction('Copy Nodes')
        self.copyFactionNodes.triggered.connect(self.menu_action)
        self.pasteFactionNodes = self.edit_menu_factions.addAction('Paste Nodes')
        self.pasteFactionNodes.triggered.connect(self.menu_action)
        self.edit_menu_factions.addSeparator()
        self.removeAllNodes = self.edit_menu_factions.addAction('Remove All Nodes')
        self.removeAllNodes.triggered.connect(self.menu_action)

        # sub-menu
        self.sub_menu_factions = QtGui.QMenu()
        self.nice = self.sub_menu_factions.addAction('Nice')
        self.nice.triggered.connect(self.menu_action)


        self.sub_menu_factions.setTitle("Great")
        self.edit_menu_factions.addMenu(self.sub_menu_factions)

I want to go through and disable all Actions, but the main QMenu.

Upvotes: 0

Views: 53

Answers (1)

HeyYO
HeyYO

Reputation: 2073

You can get a list of actions attached to a menu with actions() method. You can iterate over this list and disable them one by one.

for action in menu.actions():
    action.setDisabled(True)

Edit: this function will recursively disable menu items, but it will skip sub menus so that user can hover and see them:

def disableMenu(menu):
    for action in menu.actions():
        if action.menu():
            disableMenu(action.menu())
        else:
            action.setDisabled(True)

You can call this function on a specific menu or menuBar() to disable all menus;

disableMenu(mainWindow.menuBar())

Upvotes: 1

Related Questions