QPainter drawing lines - configuring line softness (horizontal opacity gradient)

  • Hi all!

    I am looking for a way to apply a horizontal opacity gradient when painting QLine elements using QPainter. Put simply, I want to be able to have the line opacity decrease the further away from the line center it is being painted. The effect I want to achieve corresponds to what a lot of image editing tools comonly describe as hardness of a brush. Here is a sample image that compares a line using a hard brush to a soft one.

    This would be a minimum example for painting a regular QLine:

    QPainter p;
    p.setPen(QPen(Qt::black, 12, Qt::SolidLine, Qt::RoundCap));
    p.drawLine(QPointF(0,0), QPointF(1024,1024));

    How and where would I configure the line hardness I am describing? Is there something like a fall-off property when painting QLine elements?

    In the docs I could only find examples for how to apply linear gradients between set points, which is not what I am looking for.

  • Lifetime Qt Champion

    You can apply QGradient to brush and QPen has ctor that takes a brush

  • @mrjj Thanks! I'll look into that. I got another reply from stackoverflow which I will also give a shot. I'll keep everyone in both forums posted as soon as I have some results.

Log in to reply