title: Use Clipping in Qt
body: <p>Is it possible to use clipping in an widgets <code>painEvent</code>, if the widget is using stylesheets?</p>
  
  <p>The background and reason for my question is that I want to make the widget animating when it appears and disappears. (Something like a resizing circle or square, that gets bigger starting as a small area from the center).</p>
  
  <p>My first (and only) thought on how to solve this, was to use the clipping of a <code>QPainter</code>, so that only the required area is drawn. </p>
  
  <p>If I make the Background of the widget transparent and use the primitive drawing functions from <code>QPainter</code> it works fine. But how can I solve this, if the widget has a stylesheet applied? Is it even possible?</p>
  
  <p>The used Qt version is Qt 4.8.6</p>
  
  <p><em>My questions are:</em></p>
  
  <ul>
  <li>Is it possible to achieve what I want with the mentioned strategy?</li>
  <li>Is it possible in any way to clip all the children, too?</li>
  <li>Is my strategy appropriate or is it a bad Idea to solve it that way?</li>
  <li>Are there any other ideas, best practices, Qt Classes, ... that can give me what I want?</li>
  </ul>
  
  <hr>
  
  <p><strong>Additional Information</strong></p>
  
  <p>I haven't much code to show, because I stuck with this clipping things. But here is something to get an idea of what I have tried:</p>
  
  <p>This works.</p>
  
  <pre><code>/* Shows a small red circle inside the widget as expected */
  void MyAnimatingWidget::paintEvent(QPaintEvent *ev) {
      QPainter painter(this);
      QRect rect = this-&gt;geometry()
      QStyleOption opt;
  
      painter.setClipRegion(QRegion(rect.width()/2, 
                                    rect.height()/2,
                                    150, 150, 
                                    QRegion::Ellipse));
      painter.setPen(QColor(255, 0, 0));
      painter.setBrush(QColor(255, 0, 0));
      painter.setOpacity(1);
  
      painter.drawRect(rect);
  }
  </code></pre>
  
  <p>But the following doesn't change anything:</p>
  
  <pre><code>/* This shows the widget as usual */
  void MyAnimatingWidget::paintEvent(QPaintEvent *ev) {
      QPainter painter(this);
      QRect rect = this-&gt;geometry();
      QStyleOption opt;
  
      painter.setClipRegion(QRegion(rect.width()/2, 
                                    rect.height()/2,
                                    150, 150, 
                                    QRegion::Ellipse));
      painter.setRenderHint(QPainter::Antialiasing);
      painter.setOpacity(1);
  
      opt.init(this);
      style()-&gt;drawPrimitive(QStyle::PE_Widget, &amp;opt, &amp;painter, this);
  }
  </code></pre>
  
  <p>Moreover I have noticed, that the stylesheet is also drawn, even if I remove the <code>style()-&gt;drawPrimitive(QStyle::PE_Widget, &amp;opt, &amp;painter, this);</code> line at all.</p>