1

It's quite easy to modify the default style that QGIS applies when selecting attributes in the GUI by following the steps in this question: QGIS: How to modify the appearence of selected attributes

However, when working with a stand-alone script that opens the same project, selects a feature and exports the contents of the bounding box to a png, the selection style is still the same default yellow.

For rendering and saving, I'm following the steps described in the QGIS Cookbook. The only addition is feature selection and setting extents to feature bounding box.

I probably have to set the style explicitly in a stand-alone script. How to accomplish that?

EDIT: MINIMAL REPRODUCE

Step 1: modify selected feature style in project

Step 2: load project and try to render layer with the following code

project = QgsProject.instance()
project.read('/where/my/project/at')


vlayer = project.mapLayersByName('MY_LAYER')[0]
vlayer.selectByIds([my_id])
settings = QgsMapSettings()
settings.setLayers([vlayer])
settings.setBackgroundColor(QColor(255, 255, 255))
settings.setOutputSize(QSize(800, 600))
settings.setExtent(vlayer.extent())

render = QgsMapRendererParallelJob(settings)
    image_location = os.path.join(QgsProject.instance().homePath(), "render.png")

def finished():
    img = render.renderedImage()
    # save the image; e.g. img.save("/Users/myuser/render.png","png")
    img.save(image_location, "png")

render.finished.connect(finished)

# Start the rendering
render.start()

# The following loop is not normally required, we
# are using it here because this is a standalone example.
from qgis.PyQt.QtCore import QEventLoop
loop = QEventLoop()
render.finished.connect(loop.quit)
loop.exec_()

Step 3: feature selected but selection color still standard QGIS yellow.

2
  • 2
    Could you please add a minimal working example of the script so that we can working on it? Did you try project.setSelectionColor(...)? Commented Jan 23, 2021 at 8:33
  • Sure thing! Will add code.
    – tarikki
    Commented Jan 24, 2021 at 16:24

1 Answer 1

3

In the end Kadir's comment pointed me to the right direction. Using project.setSelectionColor(...) did not change the color of the selection in the renderer but setting the style explicitly on the QgsMapSettings() did!

So by just adding settings.setSelectionColor(QColor(255, 0, 0, 50)) to my code I got the nice transparent red I was looking for.

Your Answer

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

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