Results 1 to 8 of 8

Thread: Toplevel widget with rounded corners.

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Toplevel widget with rounded corners.

    Hi,

    I have a QWidget (frameless) as my mainwindow. I want it to have rounded corners. Is there a way other than setMask() to make the corners rounded ?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Toplevel widget with rounded corners.

    What make you think that setMask() is bad way ?
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toplevel widget with rounded corners.

    I did not say it is a bad way, I was just hoping that there might be a better and simpler way to do this.

    Assuming that I use the setMask way, can you please give me a sample where i don't have to use an external image in order to set the mask.

    Thanks a lot.

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

    Default Re: Toplevel widget with rounded corners.

    Quote Originally Posted by munna
    Assuming that I use the setMask way, can you please give me a sample where i don't have to use an external image in order to set the mask.
    Notice that QWidget has 2 overloads of setMask(). One takes QBitmap and another takes QRegion. A QRegion's RegionType can be ellipse. A large enough ellipse clips corners of a rectangle and gives a rounded rectangle..
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toplevel widget with rounded corners.

    Thanks for replying

    A QRegion's RegionType can be ellipse. A large enough ellipse clips corners of a rectangle and gives a rounded rectangle
    I see your point but the problem is that I am having a square widget. This will always give a circular widget and not rounded corners.

    Is it possible to subtract four small circles from the rectangle of the widget and then set the mask ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toplevel widget with rounded corners.

    Quote Originally Posted by munna
    Is it possible to subtract four small circles from the rectangle of the widget and then set the mask ?
    QRegion::subtract()

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

    Default Re: Toplevel widget with rounded corners.

    Quote Originally Posted by munna
    Is it possible to subtract four small circles from the rectangle of the widget and then set the mask ?
    You cannot substract the circles, you will end up having holes in the corners.
    This should help unless you already solved the prob
    Qt Code:
    1. static QRegion roundedRect(const QRect& rect, int r)
    2. {
    3. QRegion region;
    4. // middle and borders
    5. region += rect.adjusted(r, 0, -r, 0);
    6. region += rect.adjusted(0, r, 0, -r);
    7. // top left
    8. QRect corner(rect.topLeft(), QSize(r*2, r*2));
    9. region += QRegion(corner, QRegion::Ellipse);
    10. // top right
    11. corner.moveTopRight(rect.topRight());
    12. region += QRegion(corner, QRegion::Ellipse);
    13. // bottom left
    14. corner.moveBottomLeft(rect.bottomLeft());
    15. region += QRegion(corner, QRegion::Ellipse);
    16. // bottom right
    17. corner.moveBottomRight(rect.bottomRight());
    18. region += QRegion(corner, QRegion::Ellipse);
    19. return region;
    20. }
    21.  
    22. someWidget->setMask(roundedRect(someWidget->rect(), 20));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. The following 3 users say thank you to jpn for this useful post:

    nshenoy (4th July 2011), sabeesh (13th November 2007), Wojtek1990 (14th August 2010)

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

    Default Re: Toplevel widget with rounded corners.

    Or yet better, I think:

    Qt Code:
    1. void RoundedWidget::resizeEvent(QResizeEvent* event)
    2. {
    3. QPixmap pixmap(size());
    4. QPainter painter(&pixmap);
    5. painter.fillRect(pixmap.rect(), Qt::white);
    6. painter.setBrush(Qt::black);
    7. painter.drawRoundRect(pixmap.rect());
    8. setMask(pixmap.createMaskFromColor(Qt::white));
    9. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. The following 4 users say thank you to jpn for this useful post:

    BatteryCell (22nd September 2007), JulesMC (17th June 2011), nitsandaniel (2nd May 2011), Wojtek1990 (14th August 2010)

Similar Threads

  1. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  2. widget with rounded corners
    By sreedhar in forum Qt Programming
    Replies: 4
    Last Post: 7th June 2006, 12:18
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.