Results 1 to 13 of 13

Thread: Display image in Qt with rounder corners

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Display image in Qt with rounder corners

    I need to display an image in Qt with curved corners. This is the obvious thing I have tried:

    Qt Code:
    1. QPixmap pix(":/1.png");
    2. fLabel->setPixmap(pix);
    3. fLabel->setStylesheet("border-radius: 5px;");
    To copy to clipboard, switch view to plain text mode 

    And it does not work. I came across a discussion https://forum.qt.io/topic/6880/image...adius-in-qml/6. It suggests a complicated solution which seems to work according to OP. However, we are not using QML in our code, so we are resigned to using stylesheets only. Is there any non-QML way to do what the code in the linked post does in QML? Or is there any other way we can show an image with rounded corners in Qt?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display image in Qt with rounder corners

    You could draw a rounded rect into a QBitmap and use that as a mask on the pixmap.

    Cheers,
    _

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

    Cupidvogel (20th November 2015)

  4. #3
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Display image in Qt with rounder corners

    Thanks. I tried this. Doesn't work.

    Qt Code:
    1. QLabel *pic = new QLabel(fParentQframe);
    2. pic->setGeometry(20,3,28,28);
    3. QPixmap pix("<path_to_a_jpeg_image>");
    4. QPixmap scaled = pix.scaled(28,28,Qt::KeepAspectRatio,Qt::SmoothTransformation);
    5.  
    6.  
    7. QBitmap map(28,28);
    8.  
    9. QPainter painter( &map );
    10. painter.setPen( Qt::transparent );
    11.  
    12. QRect rect( 0,0,28,28 );
    13. painter.drawRoundRect( 0, 0, 28, 28,8,8);
    14. painter.drawRoundRect( rect,8,8 );
    15.  
    16. pix.setMask(map);
    17.  
    18. pic->setPixmap(scaled);
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display image in Qt with rounder corners

    Hello,

    First you should fill your map with 0
    Qt Code:
    1. map.fill(Qt::color0);
    To copy to clipboard, switch view to plain text mode 
    Setting this map to the pixmap would make it completly transparent. Now set the regions that should be visible to 1. Do this by setting the painter brush to Qt::color1 and draw the rounded rectangle with this brush.
    Qt Code:
    1. painter.setBrush(Qt::color1);
    2. painter.drawRoundRect( 0, 0, 28, 28,8,8);
    To copy to clipboard, switch view to plain text mode 
    Last apply the map to the pixmap you set to the label
    Qt Code:
    1. scaled.setMask(map);
    2. pic->setPixmap(scaled);
    To copy to clipboard, switch view to plain text mode 
    In your code, you set the map to pix, but set scaled to the label.

    Best regards
    ars

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

    Cupidvogel (20th November 2015)

  7. #5
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Display image in Qt with rounder corners

    Thanks. I tried it. Same result.

    Qt Code:
    1. QLabel *pic = new QLabel(fParentQframe);
    2. pic->setGeometry(20,3,28,28);
    3. QPixmap pix("<path_to_a_jpeg_image>");
    4. QPixmap scaled = pix.scaled(28,28,Qt::KeepAspectRatio,Qt::SmoothTransformation);
    5.  
    6.  
    7. QBitmap map(28,28);
    8. map.fill(Qt::color0);
    9.  
    10. QPainter painter( &map );
    11. painter.setBrush(Qt::color1);
    12. painter.drawRoundRect( 0, 0, 28, 28,8,8);
    13.  
    14.  
    15. scaled.setMask(map);
    16. pic->setPixmap(scaled);
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display image in Qt with rounder corners

    Hello,

    use painter->roundedRect() instead of painter->roundRect(). Sorry for the typo in my previous post. I tried it on my PC and just copied text from your post and modified it

    Here is my example code:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QPixmap>
    5. #include <QBitmap>
    6. #include <QPainter>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13.  
    14. QPixmap pix(":./picture.png");
    15. QPixmap scaled = pix.scaled(28,28,Qt::KeepAspectRatio,Qt::SmoothTransformation);
    16. ui->square->setPixmap(scaled);
    17.  
    18.  
    19. QBitmap map(28,28);
    20. map.fill(Qt::color0);
    21.  
    22.  
    23. QPainter painter( &map );
    24. painter.setBrush(Qt::color1);
    25. painter.drawRoundedRect( 0, 0, 28, 28,8,8);
    26.  
    27.  
    28.  
    29. scaled.setMask(map);
    30. ui->rounded->setPixmap(scaled);
    31. }
    32.  
    33. MainWindow::~MainWindow()
    34. {
    35. delete ui;
    36. }
    To copy to clipboard, switch view to plain text mode 

    and here is the result of running it:
    Rounded.png

    Best regards
    ars

  9. The following user says thank you to ars for this useful post:

    Cupidvogel (21st November 2015)

  10. #7
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Display image in Qt with rounder corners

    He he. No problem. It works. But the rounded corners don't appear rounded like we can see with border-radius on normal elements. Instead they appear like an octagon here with eight sides..i.e the corners are not curved, but slanting down/up straight from one edge to the other. But I guess nothing can be done about it..

  11. #8
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display image in Qt with rounder corners

    Hello,

    just looking at the relatively small pictures one could think that only straight lines are cut off. Increasing the size of the picture and the radius we see that the corners are rounded. If you do not like the way roundedRect() works, you could use a user defined polygon instead. With this approach you can create nearly every form you want. As an example, the code below rounds the upper right corner of the image:
    Qt Code:
    1. QPainter painter( &map );
    2. painter.setBrush(Qt::color1);
    3. #if 0
    4. painter.drawRoundedRect( 0, 0, s,s,radius, radius);
    5. #else
    6. QPolygonF points;
    7. points.append(QPointF(0,0));
    8. points.append(QPointF(s-radius,0));
    9. for(int i=0; i<radius; ++i)
    10. {
    11. double x = s-radius+i;
    12. double y = radius-std::sqrt(radius*radius-i*i);
    13. points.append(QPointF(x,y));
    14. }
    15. points.append(QPointF(s, radius));
    16. points.append(QPointF(s,s));
    17. points.append(QPointF(0,s));
    18.  
    19. painter.drawPolygon(points);
    20. #endif
    21.  
    22. scaled.setMask(map);
    23.  
    24. QPainter scaledPainter(&scaled);
    25. scaledPainter.drawLine(QPointF(s-radius,radius), QPointF(s,radius));
    26. scaledPainter.drawLine(QPointF(s-radius,0), QPointF(s-radius,radius));
    27. ui->rounded->setPixmap(scaled);
    To copy to clipboard, switch view to plain text mode 

    It also draws vertical and horizontal lines for the corner radius (just for visualization). In the code above, s is the image size and radius the radius of the rounded corner.

    Here is an example with s = 128 and radius=20:
    Rounded.png

    Best regards
    ars

  12. The following user says thank you to ars for this useful post:

    Cupidvogel (21st November 2015)

  13. #9
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Display image in Qt with rounder corners

    Ah. That was informative! Thank you mate!

  14. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display image in Qt with rounder corners

    Btw, load into a QImage, then scale, then convert to QPixmap. Otherwise you have several conversion steps hidden in QPixmap::scaled()

    Cheer,
    _

  15. The following user says thank you to anda_skoa for this useful post:

    Cupidvogel (21st November 2015)

  16. #11
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Display image in Qt with rounder corners

    Sorry, I didn't get you. What did you mean by several steps being hidden

  17. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display image in Qt with rounder corners

    QPixmap::scaled() will convert to QImage, then scale, then convert back to QPixmap.

    Cheers,
    _

  18. #13
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Display image in Qt with rounder corners

    Oh okay. Sure.

Similar Threads

  1. Replies: 1
    Last Post: 18th October 2015, 12:06
  2. how to remove previous image to display new image in QImage
    By iswaryasenthilkumar in forum Newbie
    Replies: 6
    Last Post: 5th January 2015, 10:53
  3. Modify image to have round corners
    By migel in forum Newbie
    Replies: 1
    Last Post: 4th September 2012, 11:38
  4. Replies: 1
    Last Post: 18th January 2012, 10:00

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.