Here is a simple workaround with no Rule-based Symbolization.
Let's assume there is a polygon layer "Layer_A"
(purple) with its corresponding attribute table accordingly, see image below.
Proceed with Properties... > Symbology > Single symbol > Fill colour > Edit...
Search for colours you want to use and connect them with polygons by "id"
or any other attribute.
Schema 1. When each feature is unique
CASE
WHEN "id" = 1 THEN '#colour1'
WHEN "id" = 2 THEN '#colour2'
...
ELSE '#colourN'
END
Schema 2. When the features can be filled with the same colour
CASE
WHEN "id" IN (0,1,2,3,4) THEN '#colour1'
WHEN "id" IN (5,6,7,8,9) THEN '#colour2'
...
ELSE '#colourN'
END
The output will look as following
The same approach can be applied to Fill style
, Stroke width
, Stroke style
etc.
Moreover, it is possible to get colours randomly using the following expression:
color_rgb(rand(0, 255), rand(0, 255), rand(0, 255))
Additionally, you may try using a Python function.
In 'Function Editor'
create a new function with such code:
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
import random
@qgsfunction(args='auto', group='Custom')
def colfunc(feature, parent):
layer = iface.activeLayer()
for i in range(layer.featureCount()):
c = lambda: random.randint(0,255)
return '#{:02x}{:02x}{:02x}'.format(c(),c(),c())
Apply 'Save and Load functions'
and call this function in 'Expression window' with colfunc()
and click 'Ok'
.
So, the expected result might be looking as
References: