How can I add new actions to identify feature context menu? I mean when selecting a feature with identify tool and right click on the selected feature, here I want to add new actions to the popup menu.
-
2What type of new actions are you looking to add?– whyzarOct 16, 2017 at 16:56
-
I think that you can't add new actions to this context menu because it is created on the fly.– Fran RagaOct 18, 2017 at 7:07
-
@whyzar i want to some new menu items to do some calculations on the selected feature.– abd0991Oct 18, 2017 at 8:28
-
@FranciscoRaga have any link confirms your thoughts? thx.– abd0991Oct 18, 2017 at 8:31
-
1Update link and delete before comment. github.com/qgis/QGIS/blob/master/src/gui/…– Fran RagaOct 18, 2017 at 11:44
2 Answers
You can do that accessing the QgsMapToolIdentify.identifyMenu()
and adding a custom action to it.
To test it, just run once the following code snippet in the QGIS Python Console. Then activate the identify map tool and... voilà !
b_action_added = False # To avoid duplicating the action
def action_slot(layer, feature):
"""What our new action will do?"""
QMessageBox.information(iface.mainWindow(),
"Feature's WKT ({}:{})".format(layer.name(), feature.id()),
feature.geometry().asWkt())
def add_action(tool, old_tool):
"""Add the new action to the identify menu"""
global b_action_added
if not b_action_added and isinstance(tool, QgsMapToolIdentify):
b_action_added = True
menu = tool.identifyMenu()
my_action = QgsMapLayerAction("Show feature's WKT",
menu,
QgsMapLayerType.VectorLayer,
QgsMapLayerAction.SingleFeature)
my_action.triggeredForFeature.connect(action_slot)
menu.addCustomAction(my_action)
iface.mapCanvas().mapToolSet.connect(add_action)
Note that you can achieve the same result by creating a layer action with feature
scope:
As far as I'm aware, there is no feature in QGIS that provide you with the ability to customize the view feature form
as shown in your image below, when you identify a feature. The only features that allow for customization is to follow up with selecting a auto open form, which will provide a dialogue box that could be edit if needed or view able in conjunction with the identify console box.
You may want to look in QT designer in creating a custom form that may be helpful in finding a solution.
-
Thanks for mentioning this feature. 1+ becuase i don't know this feature.– abd0991Oct 18, 2017 at 8:33