1

QGIS uses small image preview icons for its layers in the legend and layer tree. How can I create these for a given layer as an image object (QIcon or QImage I presume)?

enter image description here

As I am looking for a PyQGIS solution, here is my existing code:

layer = iface.activeLayer()

How do I get this layer's preview icon?

1 Answer 1

1

For a QgsVectorLayer:

>>> renderer = layer.renderer()
>>> legendSymbolItems = renderer.legendSymbolItems()
>>> legendSymbolItem = legendSymbolItems[0]  # more if more classes
>>> symbol = legendSymbolItem.symbol()
>>> symbol.asImage(QSize(256, 128))
<PyQt5.QtGui.QImage object at 0x7fd869197d80>
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.