0

since we try to automate QGIS related project settings, I am currently struggling with setting project colors in the project properties usinq PyQGIS.

I managed to retrieve any existing color scheme:

project_color_scheme = qgis.core.QgsColorScheme()
for color in project_color_scheme.fetchColors():
   print(color)

For what I found out is that the method "setProjectColors" accepts an iterable to set a color scheme. But I am out of ideas how to form it.

enter image description here

Unfortunately, I did not manage to find any examples so far and as QGIS Python API lacks any examples.

How I solve this?

1 Answer 1

3

You can add the project colors using the setProjectColors method of QgsProject. The method takes a list of tuples, each of which consists of a QColor and a name.

from qgis.core import QgsProject
from PyQt5.QtGui import QColor

# define project colors as list of tuples
color1 = (QColor(100, 100, 100), 'color1')
color2 = (QColor(200, 200, 200, 100), 'color2')
colors = [color1, color2]

# set project colors
QgsProject.instance().setProjectColors(colors)

The resulting project colors:

enter image description here

0

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.