PDA

View Full Version : Freehand painting with gradient brush



olidem
5th March 2009, 18:12
Hi!

I derived from QGraphicsItem and I would like to be able to perfom some freehand painting.

For normal pens, this is not a problem, I manage a QImage and in my mouseReleaseEvent, I draw lines from one to the next event position:



void GraphicsHairstyleItem::mouseReleaseEvent(QGraphics SceneMouseEvent * event)
{
QPainter p( &backgroundImage );
QPen pen;
pen.setWidth(30);
pen.setColor(Qt::red);
p.setPen(pen);
p.drawLine(event->lastPos(),event->pos());
p.end();
}


The question now is:

How can I use a pen which is "diffuse" at the boundary, ie. a radial gradient whose alpha-component goes to 0 at the outside ???

I know that I could get a QRadial Gradient as QBrush like


QRadialGradient gradient(15, 15, 15, 15, 15);
gradient.setColorAt(0, QColor::fromRgbF(1, 1, 1, 1));
gradient.setColorAt(1, QColor::fromRgbF(1, 1, 1, 0));
QBrush b(gradient);


this does not work and as far as I know, brushes are used for filling, not for the outlines.

Appreciate any help!

Thanks a lot!!!

Olli

aamer4yu
5th March 2009, 18:22
You can create a QPainterpath for the outline and draw this path with the radial brush...
hope it works :)

olidem
5th March 2009, 18:32
But how should this painterpath look like?
Thos paths just have rectangles and bezier splines and such things.
What I would need is a rectangle, being oriented from event->lastPos() to event->pos(), with width 30 and with round ends.

I dont get a clue.

Help is still appreciated.

Thanks in advance,
Olli

talk2amulya
6th March 2009, 07:52
so u need a rounded rect? if u do, there is a addRoundedRect(..) function in QPainterPath..if not, please clear up what you want cuz u can create pretty much any shape using QPainterPath. then as aamer said, u can draw it with brush of radial gradient.

olidem
6th March 2009, 08:25
The shape I need is the shape one would get by moving such a circle-shaped brush along a straight line from A to B. If the Brush has radius r, then in essence, this would be a rectangle, which is not aligned to the x- or y-axis. Additionally, the rectangle needs round end-caps, see attachment.

Best regards,
Olli