I managed to get rounded corners on a top-level window using setMask. Here's the code for the same:
int main(int argc, char* argv[])
{
label->setPixmap(pixmap);
label->setMask(pixmap.mask());
label->setFixedSize(pixmap.width(), pixmap.height());
label->setWindowFlags(Qt::FramelessWindowHint);
label->move(100, 100);
label->show();
return a.exec();
}
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QLabel* label = new QLabel();
QPixmap pixmap("background.png");
label->setPixmap(pixmap);
label->setMask(pixmap.mask());
label->setFixedSize(pixmap.width(), pixmap.height());
label->setWindowFlags(Qt::FramelessWindowHint);
label->move(100, 100);
label->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
The background.png is transparent and has rounded corners and when i run the application, i can see rounded corners properly.
Now, the problem is how to integrate this in my code? In the above example, label is the main application widget which acts as a window, so it displays rounded image properly.
In my code, i have a custom class derived from QWidget and i display that widget using show() function. Now, when i put the above code in the constructor of my custom class, i get the background but i don't get rounded corners properly. I can see the rounded corners but the corners get filled with white color and the application appears to have rectangular corners.
Am i setting the background of my application window properly. Is this the proper way of setting background image of a window. Here's what i am doing:
1) Create a QLabel in the contructor of my class.
2) Set a background image on the QLabel.
3) Resize the QLabel to be of same size as that of the background image.
4) Resize the application window to be of same size as that of background image.
Here's the code i have:
m_pbackgroundLabel
= new QLabel(this);
m_pbackgroundLabel->setPixmap(m_pmAlbum);
m_pbackgroundLabel->setMask(m_pmAlbum.mask());
m_pbackgroundLabel->setFixedSize(m_pmAlbum.width(), m_pmAlbum.height());
// Set the window size to be same as the backround image size
setFixedSize(m_pmAlbum.width(), m_pmAlbum.height());
m_pbackgroundLabel = new QLabel(this);
m_pbackgroundLabel->setPixmap(m_pmAlbum);
m_pbackgroundLabel->setMask(m_pmAlbum.mask());
m_pbackgroundLabel->setFixedSize(m_pmAlbum.width(), m_pmAlbum.height());
// Set the window size to be same as the backround image size
setFixedSize(m_pmAlbum.width(), m_pmAlbum.height());
To copy to clipboard, switch view to plain text mode
m_pmAlbum contains the background image and is of type QPixmap.
Using setMask doesn't make a diference in my application although it works in the sample application i posted.
Bookmarks