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.


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.




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>:


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>


(This bold part you already do, but for future comers better to make it visible)


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 QWidget

setStyleSheet("background-image: url(:/images/pixmap.png);");

Where 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>.


  • Share session on subdomains in php
  • What is the difference between CacheStoreMode USE and REFRESH
  • How to copy a Shape to another worksheet (not as a picture)?
  • Copy and Paste Entire Row
  • Zipfile python module bytesize difference
  • Unable to save a query as a view table
  • How to debug 'value of ESP was not saved across function call' error?
  • How to update powerpivot pivot table filter via cell reference?
  • Circle movement upon rectangle Collision
  • Differences between drawing an Ellipse in Android and Java
  • Installing SSL on AWS EC2 Bitnami Mean Stack
  • Exception in the iconization of JInternalFrame with DefaultDesktopManager
  • Typecasting `this` of a base class template to its derived class
  • write text on image and show it to a imageview
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Web.config system.webserver errors
  • Loading .coffee files via a view in Rails
  • Inline R code in YAML for rmarkdown doesn't run
  • Create DicomImage from scratch using Dcmtk
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Sony Xperia Z Tablet not found by adb
  • Javascript convert timezone issue
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Jquery - Jquery Wysiwyg return html as a string
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • Arrays break string types in Julia
  • Cannot Parse HTML Data Using Android / JSOUP
  • WPF Applying a trigger on binding failure
  • How do you join a server to an Active Directory (domain)?
  • Java static initializers and reflection
  • File not found error Google Drive API
  • Linking SubReports Without LinkChild/LinkMaster
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • How to load view controller without button in storyboard?