Results 1 to 2 of 2

Thread: Drawing a QPixmap mit rounded corners

  1. #1
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Drawing a QPixmap mit rounded corners

    Hallo,

    I design a DAB app. Stations can be selected via pushbutton and if the right png file exists, the station logo is shown in the button(stretched to fill the button). It works but doesn't look that nice.

    Auswahl_023.jpg

    The code for each button looks like this (I'm sure it can be done better, but still learning ab Qt and C++...)

    Qt Code:
    1. if(dab_found_favs.size() >= 2){
    2. ui->btn_dab_st02->setEnabled(true);
    3. QString btn_sid = dab_vec_vec[dab_found_favs.at(1)][2];
    4. bool dab_logo_exist {MainWindow::dab_logo_exists(btn_sid)};
    5.  
    6. if(dab_logo_exist == true){
    7. ui->btn_dab_st02->setText("");
    8. QString dab_logo = path_dab_icons + btn_sid + ".png";
    9. QPixmap pixmap(dab_logo);
    10. QIcon ButtonIcon(pixmap);
    11. ui->btn_dab_st02->setIcon(ButtonIcon);
    12. ui->btn_dab_st02->setIconSize(pixmap.rect().size());
    13. } else {
    14. ui->btn_dab_st02->setText(dab_vec_vec[dab_found_favs.at(1)][0]);
    15. }
    16. } else if(dab_found_favs.size() < 2){
    17. ui->btn_dab_st02->setIcon(QIcon());
    18. ui->btn_dab_st02->setEnabled(false);
    19. ui->btn_dab_st02->setText("no favorite\navailable");
    20. }
    To copy to clipboard, switch view to plain text mode 

    How can i make the corners rounded? Already tried with QPainter, but it only drew a border with rounded corners on top of the picture.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,521
    Thanks
    252
    Thanked 721 Times in 711 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Drawing a QPixmap mit rounded corners

    Create a QBitmap mask using your rounded rectangle. The parts that you want to mask off should be set to a 0 value; everything else should be set to 1. Then use QPixmap::setMask(). Use the QBitmap as a paint device, use QPainter to fill it with white (0), then use QPainter to draw your rounded rect with a black (1) brush and pen. That's your mask.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QGraphicsRectItem rounded corners
    By forty-two in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2012, 22:44
  2. Rounded corners for a QWidget
    By Ying in forum Qt Programming
    Replies: 5
    Last Post: 2nd September 2011, 07:46
  3. How to create region with rounded corners
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2009, 09:23
  4. QDialog with rounded corners
    By rossm in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2009, 23:31
  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.