action = QAction("My Action") action.setEnabled(False)
def update_action_state(): if layer.selectedFeatureCount() > 0: action.setEnabled(True) else: action.setEnabled(False) layer.selectionChanged.connect(update_action_state) action = QAction("My Action") update_action_state()In this example, the setEnabled method is used to enable or disable the "My Action" QAction based on whether or not there are any selected features in a QGIS layer. The layer.selectionChanged signal is used to update the QAction's state whenever the selection changes.