Results 1 to 13 of 13

Thread: Display image in Qt with rounder corners

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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

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

    Cupidvogel (21st November 2015)

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
  •  
Qt is a trademark of The Qt Company.