title: Subclassing QGraphicsRectItem in PyQt
body: <p>I am writing an application in which I want to be able to move rectangles by clicking and dragging them. The code below does this correctly so far</p>
  
  <pre><code>from PyQt5.QtCore import *
  from PyQt5.QtGui import *
  from PyQt5.QtWidgets import *
  
  import sys
  
  class Memory(QWidget):
  
      grid = None
      scene = None
  
      def __init__(self, geometry, parent=None):
          super(Memory, self).__init__(parent=parent)
  
          self.layout = QGridLayout(self)
  
          self.scene = QGraphicsScene(self)
          self.scene.setSceneRect(geometry.x(), geometry.y(), geometry.width(), geometry.height())
          self.scene.setBackgroundBrush(Qt.white)
  
          self.view = QGraphicsView()
          self.view.setScene(self.scene)
  
          self.layout.addWidget(self.view)
  
          rect = QRectF(40, 40, 220, 110)
          self.make_proxy(rect)
  
          rect2 = QRectF(300, 40, 220, 110)
          self.make_proxy(rect2)
  
      def make_proxy(self, rect):
  
          label = QLabel('World')
          label.setAlignment(Qt.AlignCenter | Qt.AlignCenter)
  
          # create proxy
          proxy = QGraphicsProxyWidget()
          proxy.setWidget(label)
          proxy.setGeometry(rect)
          self.scene.addItem(proxy)
  
          # create parent item
          rectangle = QGraphicsRectItem(rect)
          rectangle.setFlag(QGraphicsItem.ItemIsMovable, True)
          self.scene.addItem(rectangle)
  
          # set parent
          proxy.setParentItem(rectangle)
  
  
  def main():
  
      app = QApplication(sys.argv)
      form = Memory(QRect(10, 10, 550, 180))
      form.show()
      app.exec_()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>However, the first rectangle is always on the bottom of the scene since it was added at first to it. But when I click on it it should go to the top of the scene so that it appears above the second rectangle. This is why I want to subclass <code>QGraphicsRectItem</code> and add new features to it. But when I implement it as below </p>
  
  <pre><code>class RectItem(QGraphicsRectItem):
  
      def __init__(self, rect):
          QGraphicsRectItem.__init__(self)
          self.rectangle = rect
  
      def boundingRect(self):
          super(RectItem, self).boundingRect()
          return self.rectangle
  </code></pre>
  
  <p>and define the <code>rectangle</code> variable as</p>
  
  <pre><code>        rectangle = RectItem(rect)
  </code></pre>
  
  <p>the items are not movable anymore, i.e. the <code>QGraphicsItem.ItemIsMovable</code> flag has no effect. </p>
  
  <p>Is there a simple way of subclassing <code>QGraphicsRectItem</code> and still keep all the convenience functions like <code>setFlag(QGraphicsItem.ItemIsMovable, True)</code> working?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As eyllanesc pointed out it is not necessary to overwrite the <code>boundingRect</code> method. Still, when I try to subclass <code>QGraphicsRectItem</code> as</p>
  
  <pre><code>class RectItem(QGraphicsRectItem):
  
      def __init__(self, rect):
          QGraphicsRectItem.__init__(self)
  </code></pre>
  
  <p>and define <code>rectangle</code> as </p>
  
  <pre><code>        rectangle = RectItem(rect)
  </code></pre>
  
  <p>I don't get the same behavior as with </p>
  
  <pre><code>        rectangle = QGraphicsRectItem(rect)
  </code></pre>
  
  <p>What is the reason for this?</p>