Results 1 to 5 of 5

Thread: Freehand painting with gradient brush

  1. #1
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Freehand painting with gradient brush

    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:

    Qt Code:
    1. void GraphicsHairstyleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
    2. {
    3. QPainter p( &backgroundImage );
    4. QPen pen;
    5. pen.setWidth(30);
    6. pen.setColor(Qt::red);
    7. p.setPen(pen);
    8. p.drawLine(event->lastPos(),event->pos());
    9. p.end();
    10. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. QRadialGradient gradient(15, 15, 15, 15, 15);
    2. gradient.setColorAt(0, QColor::fromRgbF(1, 1, 1, 1));
    3. gradient.setColorAt(1, QColor::fromRgbF(1, 1, 1, 0));
    4. QBrush b(gradient);
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Freehand painting with gradient brush

    You can create a QPainterpath for the outline and draw this path with the radial brush...
    hope it works

  3. #3
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Freehand painting with gradient brush

    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

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Freehand painting with gradient brush

    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.

  5. #5
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Freehand painting with gradient brush

    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
    Attached Images Attached Images
    Last edited by olidem; 6th March 2009 at 08:33.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.