From a cartographic point of view, it should be said that if you can't readily distinguish visually between categories, then there is not much point to having the categorized style in the first place. So having a separate category for every feature id in a layer probably doesn't make a lot of sense and simply labelling the layer with feature id (as per @Esmaeel's answer) is a better approach. However, I found the question of highlighting a feature on the map canvas when its category is selected in the layer tree an interesting exercise. So here is a way to achieve it fairly easily with some pyqgis. This can be run directly from the Python console for testing, but for the script to work properly without modification, the layer must be categorized with the value $id. You would also need to modify it a bit to work in a plugin. The basic idea is to access the
QItemSelectionModel() object of the layer tree view and connect to its
selectionChanged() signal. In the slot function, we access the current layer and add a condition to isolate the logic to only that layer, using the layer name. We then access the current legend node and return its data item with index 2 which, in this case, corresponds to a feature id. We can then use the
flashFeatureIds() method of
QgsMapCanvas() to blink the feature on the canvas.
Example code snippet:
lyr = iface.layerTreeView().currentLayer()
lyr_name = lyr.name()
if lyr_name == 'newgen':
current_legend_node = iface.layerTreeView().currentLegendNode()
if current_legend_node is not None:
feature_id = current_legend_node.data(2)
To disconnect the slot from the signal, use the following:
Link to docs for flashFeatureIds(). You can pass additional arguments to change the start color, end color, number of flashes and duration.