The point of rule-based labeling is that each label has a "rule" or filter, so it only displays in certain situations. In this case, you want the smaller black labels to display only when the road is long enough for all three labels, you want the larger magenta to display only when the road is long enough for both two labels, and you want the green labels always.
Put filter expressions in the text box called "filter" ("Suodatin" in your version of QGIS)
To test whether the road is long enough for all three labels, we must compare the length of the road with the length of the labels.
- Measure the road length with this function:
Measure the length of the labels with this expression (where "name1" "name2" and "name3 are the attribute table fields used for the labels):
length("name1") + length("name2") + 0.8*length("name3")`.
length("name3") is multiplied by a conversion factor (in this case 0.8) because the "name3" label is a smaller font than the other two. Calculate your own conversion factor by dividing the font size of the "name3" label by the font size of the other two labels.
For the top priority (green) labels, use no filter expression
For the medium priority (magenta) labels, use a filter like this:
$length > (length("name1") + length("name2"))*50
Where that expression says
50, substitute a value that converts the label length into the same units as the road length. It's probably easier to find the right value by trial and error than to try to calculate it. If you want the labels to work at different map scales, include the
For the smaller black labels, use a filter expression like this:
$length > (length("name1") + length("name2") + 0.8*length("name3"))*50
Note: Ignore the formulas in the screenshots. I heavily edited this answer based on feedback, so the screenshots don't match the formulas in the text.
Here's some unsolicited advice. Feel free to ignore it.
I find the green labels on your map hard to read. They're just a bit too bright of a green, and the gaps in the white background don't help. I suggest using a slightly darker green, and using a solid white background or white text buffer.