Search code examples
windowsqtqwidget

How to make QWidget background with specified png file?


MyDialog::MyDialog(QWidget* parent, Qt::WindowFlags f)
    : QWidget(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)    
    , _pixmap(new QPixmap(myPngFile))
{
    QPalette palette;
    palette.setBrush(this->backgroundRole(), QBrush(*_pixmap));
    this->setPalette(palette);

    setFixedSize(_pixmap->size());
 }

myPngFile define the png path. The problem is the transparent part in png file showed black when I show MyDialog, how do I correct it to load myPngFile?

I am using Windows platform with Qt4.8

Please do not use stylesheet.


Solution

  • If you really don't want to use style sheets, your problem could be solved by overwriting the paint event of your MyDialog class like in the answer of this stackoverflow question: Background image not showing on QWidget

    But I would also recommend using style sheets for your problem.