title: Qt Translucent background causes child widget to be 'imprinted' in parent
body: <p>I have a parent container (<code>MyCartParentWidget</code>) with translucent background, inside which I have to draw a child widget (<code>MyCart</code>) with an image background (<a href="https://imgur.com/RnSghvV" rel="nofollow noreferrer">this image</a> is in portrait, <a href="https://imgur.com/ckngjNH" rel="nofollow noreferrer">this image</a> is in landscape), also drawn with translucent background, and both being <code>QLabels</code>. There is a button clicking on which, the child widget toggles its dimensions (<code>resetCartStyle</code>), i.e it goes from portrait to landscape mode and vice versa. Problem is, when it toggles, the original imprint stays back, i.e, this is the original pic where it is in 'portrait' mode:</p>
  
  <p><a href="https://i.stack.imgur.com/rdTG2.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rdTG2.jpg" alt="enter image description here"></a></p>
  
  <p>Then when I switch to 'landscape' mode, it does shift, but the original 'portrait' mode stays back:</p>
  
  <p><a href="https://i.stack.imgur.com/CXWgv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CXWgv.jpg" alt="enter image description here"></a></p>
  
  <p>This is my code:</p>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include "MyCartParentWidget.hpp"
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MyCartParentWidget p;
      p.move(370,10);
      p.show();
      return a.exec();
  }
  </code></pre>
  
  <p>MyCart.cpp:</p>
  
  <pre><code> #include "MyCart.hpp"
   #include &lt;QPainter&gt;
  
  MyCart::MyCart(QWidget *parent): QLabel(parent)
  {
      setAttribute(Qt::WA_TranslucentBackground);
      fPixMap.load("/Users/attitude/Desktop/RnSghvV.png");
      setStyleSheet("background-color: rgba(0,0,0,255);");
      setFixedSize(325,400);
  
  }
  
  void MyCart::paintEvent(QPaintEvent *)
  {
      QPainter p(this);
      p.setRenderHint(QPainter::SmoothPixmapTransform);
      p.drawPixmap(0,0,width(),height(),fPixMap);
  }
  
  void MyCart::resetCartStyle(QString url, int w, int h)
  {
      setFixedSize(w,h);
      fPixMap.load(url);
      this-&gt;update();
  }
  </code></pre>
  
  <p>MyCart.hpp:</p>
  
  <pre><code>#pragma once
  
  #include &lt;QLabel&gt;
  #include &lt;QPaintEvent&gt;
  #include &lt;QPixmap&gt;
  
  
  class MyCart: public QLabel
  {
  public:
      MyCart(QWidget*);
      virtual void paintEvent(QPaintEvent *);
      QPixmap fPixMap;
      void resetCartStyle(QString, int w, int h);
  };
  </code></pre>
  
  <p>MyCartParentWidget.cpp:   </p>
  
  <pre><code>#include "MyCartParentWidget.hpp"
  #include &lt;QPushButton&gt;
  
  MyCartParentWidget::MyCartParentWidget()
  {
  
      setFixedSize(800,700);
      setWindowFlags(Qt::FramelessWindowHint);
      setAttribute(Qt::WA_TranslucentBackground);
      setStyleSheet("background-color: none;");
  
      fLayout = new QHBoxLayout();
      setLayout(fLayout);
      fLayout-&gt;setContentsMargins(0,0,0,0);
      fLayout-&gt;setSpacing(0);
      fLayout-&gt;setMargin(0);
      fLayout-&gt;setAlignment(Qt::AlignLeft | Qt:: AlignTop);
      i = 0;
  
      fCart = new MyCart(this);
      fLayout-&gt;addWidget(fCart);
  
      QPushButton* p = new QPushButton(this);
      p-&gt;setText("Toggle");
      p-&gt;move(0,650);
      connect(p,SIGNAL(clicked(bool)),this,SLOT(clickedSlot()));
  }
  
  void MyCartParentWidget::clickedSlot()
  {
      if (i == 0)
      {
          //enter landscape mode
          i = 1;
          fCart-&gt;resetCartStyle("/Users/attitude/Desktop/foo.png",400,325);
      }
      else
      {
          //enter portrait mode
          i = 0;
          fCart-&gt;resetCartStyle("/Users/attitude/Desktop/RnSghvV.png",325,400);
      }
  }
  </code></pre>
  
  <p>MyCartParentWidget.hpp:</p>
  
  <pre><code>#pragma once
  
  #include &lt;QLabel&gt;
  #include &lt;QHBoxLayout&gt;
  #include "MyCart.hpp"
  
  
  class MyCartParentWidget: public QLabel
  {
      Q_OBJECT
  public:
      MyCartParentWidget();
  
      QHBoxLayout* fLayout;
      MyCart *fCart;
      int i;
  
  private slots:
      void clickedSlot();
  };
  </code></pre>
  
  <p>This problem does not happen when I set the background of the parent widget to something like <code>green</code> and comment out the <code>setAttribute(Qt::WA_TranslucentBackground);</code> part, this happens only with <code>setAttribute(Qt::WA_TranslucentBackground);</code> part.</p>
  
  <p>How do I fix this?</p>
  
  <p>Platform - OS X Yosemite, Qt 5.3.1, 32 bit.</p>
  
  <p>Ilya's solution below works fine on Windows, but the problem persists on Mac.</p>