PDA

View Full Version : Drawing a QPixmap mit rounded corners



vitalic
5th May 2020, 19:32
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.

13430

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



if(dab_found_favs.size() >= 2){
ui->btn_dab_st02->setEnabled(true);
QString btn_sid = dab_vec_vec[dab_found_favs.at(1)][2];
bool dab_logo_exist {MainWindow::dab_logo_exists(btn_sid)};

if(dab_logo_exist == true){
ui->btn_dab_st02->setText("");
QString dab_logo = path_dab_icons + btn_sid + ".png";
QPixmap pixmap(dab_logo);
QIcon ButtonIcon(pixmap);
ui->btn_dab_st02->setIcon(ButtonIcon);
ui->btn_dab_st02->setIconSize(pixmap.rect().size());
} else {
ui->btn_dab_st02->setText(dab_vec_vec[dab_found_favs.at(1)][0]);
}
} else if(dab_found_favs.size() < 2){
ui->btn_dab_st02->setIcon(QIcon());
ui->btn_dab_st02->setEnabled(false);
ui->btn_dab_st02->setText("no favorite\navailable");
}


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.

d_stranz
5th May 2020, 20:54
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.