Disable menu item unless exactly one item is selected with PySide
Question: Working with a [`QMenu`](http://doc.qt.io/qt-4.8/qmenu.html) in PySide, I want
to disable a menu item based on a
[`QListWidget`](http://doc.qt.io/qt-4.8/qlistwidget.html) selection count. If
the selection count is exactly one then the 'Edit Item' action should be
enabled, otherwise it should be disabled. How can I do this?

    
    
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    # Modules
    # ------------------------------------------------------------------------------
    import sys
    from PySide import QtGui, QtCore
    
    # Variables
    # ------------------------------------------------------------------------------
    listItems = ["A","B","C","D","E","F","G"]
    
    # widget
    # ------------------------------------------------------------------------------
    class Example(QtGui.QWidget):
    
        def __init__(self,):
            super(Example, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            # formatting
            self.setGeometry(300, 300, 250, 300)
            self.setWindowTitle("Input List")
    
            # widgets
            self.itemList = QtGui.QListWidget()
            self.itemList.addItems(listItems)
            self.itemList.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
    
            # context menu
            self.edit_menu = QtGui.QMenu()
            removeItem = self.edit_menu.addAction('Remove Item')
            removeItem.triggered.connect(self.RemoveItem)
    
            editItem = self.edit_menu.addAction('Edit Item')
            editItem.triggered.connect(self.EditItem)
    
            self.itemList.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
            self.itemList.customContextMenuRequested.connect(self.on_context_menu)
            self.itemList.itemDoubleClicked.connect(self.EditItem)
    
            # layout
            self.mainLayout = QtGui.QGridLayout(self)
            self.mainLayout.addWidget(self.itemList, 0, 0)
            self.show()       
    
        def on_context_menu(self, pos):
            self.edit_menu.exec_(self.mapToGlobal(pos))
    
        def EditItem(self):
            print "Edit Item"
    
        def RemoveItem(self):
            print "Remove Item"
    
    # Main
    # ------------------------------------------------------------------------------
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    


Answer: There are a few changes you need to make, but you can do this by disabling the
[`QAction`](http://doc.qt.io/qt-4.8/qaction.html#enabled-prop).

First change, make your `editItem` variable accessible throughout the class.

    
    
    self.editItem = self.edit_menu.addAction('Edit Item')
    self.editItem.triggered.connect(self.EditItem)
    

(You may want to do this to the `remoteItem` as well, for consistency)

Next, in `on_context_menu`, you need to check how many items you have
selected. Then you enable/disable your menu option based on that logic:

    
    
    def on_context_menu(self, pos):
        if len(self.itemList.selectedItems()) == 1:
            self.editItem.setEnabled(False)
        else:
            self.editItem.setEnabled(True)
        self.edit_menu.exec_(self.mapToGlobal(pos))
    

* * *

Examples:

With only one item selected:

[![One item
selected](http://i.stack.imgur.com/2qJO1.png)](http://i.stack.imgur.com/2qJO1.png)

With more than one item selected:

[![More than one item
selected](http://i.stack.imgur.com/EJsbC.png)](http://i.stack.imgur.com/EJsbC.png)

