MyDialog::MyDialog(QWidget* parent, Qt::WindowFlags f)
: QWidget(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
, _pixmap(new QPixmap(myPngFile))
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.Answer1:
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:
<a href="https://stackoverflow.com/questions/2824447/background-image-not-showing-on-qwidget" rel="nofollow">Background image not showing on QWidget</a>
But I would also recommend using style sheets for your problem.Answer2:
Take a look at <a href="http://qt-project.org/forums/viewthread/3155" rel="nofollow">this:</a>
QPixmap aPixmap(":/splash.png"); QLabel* aWidget = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint); aWidget->setAttribute(Qt::WA_TranslucentBackground); aWidget->setPixmap(aPixmap); aWidget->show();
And <a href="http://qt-project.org/doc/qt-5.0/qtwidgets/qwidget.html#transparency-and-double-buffering" rel="nofollow">docs</a>:<blockquote>
Creating Translucent Windows Since Qt 4.5, it has been possible to create windows with translucent regions on window systems that support compositing. To enable this feature in a top-level widget, set its Qt::WA_TranslucentBackground attribute with setAttribute() and ensure that its background is painted with non-opaque colors in the regions you want to be partially transparent.
Platform notes: X11: This feature relies on the use of an X server that supports ARGB visuals and a compositing window manager. <strong>Windows: The widget needs to have the Qt::FramelessWindowHint window flag set for the translucency to work.</strong></blockquote>
(This bold part you already do, but for future comers better to make it visible)Answer3:
I cannot understand why you do not want to use <a href="http://qt-project.org/doc/qt-4.8/stylesheet.html" rel="nofollow">stylesheets</a> since this it the preferred way. Use the <a href="http://qt-project.org/doc/qt-5.0/qtwidgets/qwidget.html#styleSheet-prop" rel="nofollow">
setStylesheet</a> method of
pixmap.png will be located in the resource file under the
images prefix. For more details check the <a href="http://qt-project.org/doc/qt-4.8/stylesheet-examples.html" rel="nofollow">Qt stylesheet examples</a>.