Results 1 to 3 of 3

Thread: QPainter - drawRoundedRect Question

  1. #1
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation QPainter - drawRoundedRect Question

    Hello All,

    I was wondering if there was a way in which to draw a rounded rectangle using the drawRoundedRect() function on a QPainter object in which half of the Rounded Rectangle is a Gradient and the other half is a solid color.

    Thanks Alot.

    Chris

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPainter - drawRoundedRect Question

    I'm afraid you have to do it in two parts by clipping half of the other rounded rect. See QPainter::setClipRect().
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    ChrisReath (14th May 2008)

  4. #3
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter - drawRoundedRect Question

    If anyone is interested, here's is how I solved the problem mentioned above with Clipping. I start with a (60, 60) rendering area. My objective is to paint half of a Rounded Rectangle a solid color while the other half is a gradient. I split the Rounded Rectangle from from (0, 60) to (60,0).

    First I declare the two regions. The Upper Region and the Lower Region

    Qt Code:
    1. QPainterPath UpperSidePath;
    2. QPolygonF UpperPolygon;
    3. UpperPolygon << QPointF(0,0) << QPointF(60,0) << QPointF(0,60);
    4. HighSidePath.addPolygon(UpperPolygon);
    5.  
    6. QQPainterPath LowSidePath;
    7. QPolygonF LowerPolygon;
    8. LowerPolygon << QPointF(60,0) << QPointF(0,60) << QPointF(60,60);
    9. LowSidePath.addPolygon(LowerPolygon);
    To copy to clipboard, switch view to plain text mode 

    Next, I paint the two separate regions using clipping. (The Upper Region is painted in red, the lower region using a green gradient.

    Qt Code:
    1. QPainter UpperRegion;
    2. UpperRegion.begin(this);
    3. UpperRegion.setRenderHint(QPainter::Antialiasing);
    4. UpperRegion.scale(width() / 60, height() / 60);
    5. UpperRegion.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    6. UpperRegion.setClipping(true);
    7. UpperRegion.setClipPath(UpperSidePath);
    8. UpperRegion.setBrush(QColor(0xff, 0x00, 0x04));
    9. UpperRegion.drawRoundedRect(rectangle, 20.0, 15.0);
    10. UpperRegion.drawLine(line);
    11. UpperRegion.end();
    12.  
    13. QPainter LowerRegion;
    14. LowerRegion.begin(this);
    15. LowerRegion.setRenderHint(QPainter::Antialiasing);
    16. LowerRegion.scale(width() / 60, height() / 60);
    17. LowerRegion.setPen(QPen(Qt::blue, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    18. LowLoadedGradient.setColorAt(0.0, Qt::white);
    19. LowLoadedGradient.setColorAt(0.2, Qt::green);
    20. LowLoadedGradient.setColorAt(1.0, Qt::black);
    21. LowerRegion.setBrush(LowLoadedGradient);
    22. LowerRegion.setClipping(true);
    23. LowerRegion.setClipPath(LowSidePath);
    24. LowerRegion.drawRoundedRect(rectangle, 20.0, 15.0);
    25. LowerRegion.end();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QPainter Question
    By atb in forum Qt Programming
    Replies: 3
    Last Post: 18th March 2008, 12:28
  2. QPainter bounds question
    By Micawber in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2007, 15:09
  3. QPainter, setWindow, setViewport question
    By Micawber in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2007, 19:33
  4. Question on QPainter setWindow use
    By impeteperry in forum Qt Programming
    Replies: 14
    Last Post: 8th August 2006, 14:26
  5. Replies: 7
    Last Post: 20th March 2006, 21:03

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.