Results 1 to 6 of 6

Thread: Rounded corners for a QWidget

  1. #1
    Join Date
    Aug 2011
    Location
    Austria/Germany
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Rounded corners for a QWidget

    Hi all
    I am new to this great and helpful forum! I solved many problems with solutions here posted. So here is my contribution:

    Actually we are programming a login manager for Linux/Unix systems: LINK
    We are now in a state, where we are concentrating on the theme management and style. Therefore I needed a function to create a widget with rounded corners. But I wanted to have the ability to give each corner a different radius. I found here some small code examples, but only for adjusting all corners with the same radius. So, I want to fill this gap. Here is my code example, to give a widget rounded corners, with the ability to give each corner a different radius:

    Qt Code:
    1. void Widget::setRoundedCorners(int radius_tl, int radius_tr, int radius_bl, int radius_br) {
    2. QRegion region(0, 0, width(), height(), QRegion::Rectangle);
    3.  
    4. // top left
    5. QRegion round (0, 0, 2*radius_tl, 2*radius_tl, QRegion::Ellipse);
    6. QRegion corner(0, 0, radius_tl, radius_tl, QRegion::Rectangle);
    7. region = region.subtracted(corner.subtracted(round));
    8.  
    9. // top right
    10. round = QRegion(width()-2*radius_tr, 0, 2*radius_tr, 2*radius_tr, QRegion::Ellipse);
    11. corner = QRegion(width()-radius_tr, 0, radius_tr, radius_tr, QRegion::Rectangle);
    12. region = region.subtracted(corner.subtracted(round));
    13.  
    14. // bottom right
    15. round = QRegion(width()-2*radius_br, height()-2*radius_br, 2*radius_br, 2*radius_br, QRegion::Ellipse);
    16. corner = QRegion(width()-radius_br, height()-radius_br, radius_br, radius_br, QRegion::Rectangle);
    17. region = region.subtracted(corner.subtracted(round));
    18.  
    19. // bottom left
    20. round = QRegion(0, height()-2*radius_bl, 2*radius_bl, 2*radius_bl, QRegion::Ellipse);
    21. corner = QRegion(0, height()-radius_bl, radius_bl, radius_bl, QRegion::Rectangle);
    22. region = region.subtracted(corner.subtracted(round));
    23.  
    24. setMask(region);
    25. }
    To copy to clipboard, switch view to plain text mode 


    Roland
    Last edited by Ying; 31st August 2011 at 21:20.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Rounded corners for a QWidget

    Well, style sheets do all that for you, and offer you a greater flexibility, because you don't have to implement your style in code, and can therefore Style the same exe any way you like with Stylesheets with out the need to rebuild.
    Have a look at border-radius here:
    http://doc.qt.nokia.com/latest/style...reference.html

    EDIT:
    However, the style sheet border-radius is only supported by some widgets.
    You solution is good for those not supported.
    Last edited by high_flyer; 1st September 2011 at 10:16.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2011
    Location
    Austria/Germany
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Maemo/MeeGo

    Default Re: Rounded corners for a QWidget

    QWidget:

    Supports only the background, background-clip and background-origin properties.

    If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below....
    @high_flyer: Yeah the style sheets is a nice thing, but for a simple QWidget useless.

    Roland

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rounded corners for a QWidget

    Quote Originally Posted by Ying View Post
    @high_flyer: Yeah the style sheets is a nice thing, but for a simple QWidget useless.
    why should it be useless?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Rounded corners for a QWidget

    In the sense, that QWidget does not support rounded corners over Stylesheets, as I added in the last section in post #2.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. The following user says thank you to high_flyer for this useful post:

    FelixB (2nd September 2011)

  7. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rounded corners for a QWidget

    did I mention I hate edits?

    thanks, didn't know that.

Similar Threads

  1. Replies: 6
    Last Post: 14th September 2009, 16:56
  2. How to create region with rounded corners
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2009, 09:23
  3. QDialog with rounded corners
    By rossm in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2009, 23:31
  4. Toplevel widget with rounded corners.
    By munna in forum Qt Programming
    Replies: 7
    Last Post: 18th August 2006, 20:46
  5. widget with rounded corners
    By sreedhar in forum Qt Programming
    Replies: 4
    Last Post: 7th June 2006, 12:18

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.