title: Make QPainter paint on top of a content which is inside a QScrollArea and has been scrolled, in the correct position
body: <p>I'm doing something cool with a custom <code>QWebView</code>, basically I paint a rectangle over the element which is under the mouse pointer, see:</p>
  
  <p><img src="https://i.stack.imgur.com/rtKmp.png" alt="cool stuff"></p>
  
  <p>However, if I scroll down and move the pointer again, it draws the rect on the wrong area. In the next screenshot the mouse is over the  which contains the search results, the top of the rectangle and the top of the <code>QWebView</code> have a big gap:</p>
  
  <p><img src="https://i.stack.imgur.com/bCzh5.png" alt="the big gap"></p>
  
  <p>What I did to insert this bug was simply calculating the position of the rect taking into account the horizontal and vertical scroll positions, I did this wrong calculation because the rect wasn't correctly positioned in regard to the content if I scrolled it. In both cases I have a rect painted in the wrong position if any scroll occurs.</p>
  
  <p>So far, this is the code I have at the moment:</p>
  
  <pre><code>#!/usr/bin/env python2
  # coding: utf-8
  #                        VENI, SANCTE SPIRITUS
  
  from PySide.QtWebKit import QWebView
  from PySide import QtCore, QtGui
  
  
  class CustomQWebView(QWebView):
  
      def __init__(self, *args, **kwargs):
          """ Init the custom class
          """
          super(CustomQWebView, self).__init__(*args, **kwargs)
          self.currentframe = None
          self.lastelement = None
          self.lastelementboundingrect = None
          self.loadFinished.connect(self.setframeafterloadfinished)
          # default test pen
          self.defaultpen = QtGui.QPen(QtCore.Qt.blue)
          self.defaultpen.setWidth(3)
  
      @QtCore.Slot(QtGui.QMouseEvent)
      def mouseMoveEvent(self, event):
          super(CustomQWebView, self).mouseMoveEvent(event)
  
          if self.currentframe:
              hittestresult = self.currentframe.hitTestContent(event.pos())
              element = hittestresult.element()
              if self.lastelement != element:
                  self.lastelement = element
                  self.lastelementboundingrect = hittestresult.boundingRect()
                  self.update()
  
      @QtCore.Slot(QtGui.QPaintEvent)
      def paintEvent(self, event):
          # draw the content first
          super(CustomQWebView, self).paintEvent(event)
  
          # then the rectangle
          if self.lastelementboundingrect:
              painter = QtGui.QPainter(self)
              painter.setPen(self.defaultpen)
              # This rectangles takes into account any offset of the scroll bar
              # so the rectangle can be drawn correctly
              rect = QtCore.QRect()
              rect.setRect(self.lastelementboundingrect.x() +
                           self.currentframe.scrollPosition().x(),
                           self.lastelementboundingrect.y() +
                           self.currentframe.scrollPosition().y(),
                           self.lastelementboundingrect.width(),
                           self.lastelementboundingrect.height())
              # painter.drawRect(QtCore.QRectF(self.lastelementcurrectrect))
              painter.fillRect(
                  QtCore.QRectF(rect), QtGui.QColor(255, 165, 0, 128))
  
      def setframeafterloadfinished(self):
          self.currentframe = self.page().mainFrame()
  </code></pre>
  
  <p>How can I correctly solve this positioning problem?</p>
  
  <p>P.S.: Yes, the pointer don't appears in the screenshots because scrot cannot capture it, I did not realize before uploading the screenshots. Sorry.</p>