Are you sure your PNG file is actually transparent? The following (which is essentially what you are doing) works for me. If this fails on your computer, perhaps indicate which version of Qt you are using and which platform.
#include <QtGui> class TransparentWidget : public QWidget { public: TransparentWidget() : QWidget(), background_pixmap_(":/semi_transparent.png") { setFixedSize(400, 100); } protected: void paintEvent(QPaintEvent *) { QPainter painter(this); int x = 0; while (x < width()) { painter.drawPixmap(x, 0, background_pixmap_); x += background_pixmap_.width(); } } private: QPixmap background_pixmap_; }; class ParentWidget : public QWidget { public: ParentWidget() : QWidget() { QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(new TransparentWidget); layout->addWidget(new QPushButton("Button")); setLayout(layout); setBackgroundRole(QPalette::Dark); setAutoFillBackground(true); } }; int main(int argc, char **argv) { QApplication app(argc, argv); ParentWidget w; w.show(); return app.exec(); }
source share