title: How do I add a custom QGraphicsItem to my QGraphicsScene
body: <p>I have this widget that I would like to add to my qgraphicsscene but it isn't working exactly how I want it to. I think the problem has to do with the parent of the widget. My goal is to be able to add the widget to the scene and move it freely.</p>
  <p>I have this widget called twoPin that looks like this.</p>
  <p><img src="https://i.stack.imgur.com/MTr1v.png" alt="Custom Widget" /></p>
  <p>And here is the code that I had generated from QtDesigner and updated.</p>
  <pre><code>class Ui_Form(QtWidgets.QGraphicsItem):
      def __init__(self, Form):
          super().__init__()
          Form.setObjectName(&quot;Form&quot;)
          Form.resize(150, 95)
          sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
          sizePolicy.setHorizontalStretch(0)
          sizePolicy.setVerticalStretch(0)
          sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
          Form.setSizePolicy(sizePolicy)
          Form.setMinimumSize(QtCore.QSize(150, 95))
          Form.setMaximumSize(QtCore.QSize(168, 16777215))
          self.label_name = QtWidgets.QLabel(Form)
          self.label_name.setGeometry(QtCore.QRect(0, 0, 150, 20))
          self.label_name.setMinimumSize(QtCore.QSize(0, 20))
          self.label_name.setMaximumSize(QtCore.QSize(16777215, 20))
          self.label_name.setAlignment(QtCore.Qt.AlignCenter)
          self.label_name.setObjectName(&quot;label_name&quot;)
          self.frame = QtWidgets.QFrame(Form)
          self.frame.setGeometry(QtCore.QRect(0, 20, 150, 75))
          self.frame.setMinimumSize(QtCore.QSize(150, 50))
          self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
          self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
          self.frame.setObjectName(&quot;frame&quot;)
          self.lable_image = QtWidgets.QLabel(self.frame)
          self.lable_image.setGeometry(QtCore.QRect(0, 0, 150, 75))
          self.lable_image.setText(&quot;&quot;)
          self.lable_image.setScaledContents(True)
          self.lable_image.setObjectName(&quot;lable_image&quot;)
          self.btn_pin0 = QtWidgets.QPushButton(self.frame)
          self.btn_pin0.setGeometry(QtCore.QRect(0, 0, 75, 75))
          self.btn_pin0.setStyleSheet(&quot;&quot;)
          self.btn_pin0.setText(&quot;&quot;)
          self.btn_pin0.setObjectName(&quot;btn_pin0&quot;)
          self.btn_pin1 = QtWidgets.QPushButton(self.frame)
          self.btn_pin1.setGeometry(QtCore.QRect(75, 0, 75, 75))
          self.btn_pin1.setStyleSheet(&quot;&quot;)
          self.btn_pin1.setText(&quot;&quot;)
          self.btn_pin1.setObjectName(&quot;btn_pin1&quot;)
  
          self.retranslateUi(Form)
          QtCore.QMetaObject.connectSlotsByName(Form)
  
          self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
  
      def retranslateUi(self, Form):
          _translate = QtCore.QCoreApplication.translate
          Form.setWindowTitle(_translate(&quot;Form&quot;, &quot;Form&quot;))
          self.label_name.setText(_translate(&quot;Form&quot;, &quot;Component Name&quot;))
  
      def boundingRect(self):
          return QtCore.QRectF(0, 0, 150, 95)
  
      def paint(self, painter, options, widget):
          painter.drawRect(0, 0, 150, 95)
  </code></pre>
  
  <p>I have my main window that consists of a qgraphicsview and qgraphicsscene and I want to add my custom widget to it. When I try to add the widget, I don't know which component to make its parent. If I try to make the graphicsview the parent, I can see the boundingbox in the scene but the widget itself is stuck to the top corner of the window without being able to move it. Here is the code and an image to show.</p>
  <p>Main Window</p>
  <p><img src="https://i.stack.imgur.com/XCHor.png" alt="" /></p>
  <pre><code>from PySide2 import QtCore, QtGui, QtWidgets
  from PySide2.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime, QMetaObject, QObject, QPoint, QRect, QSize, QTime, QUrl, Qt, QEvent)
  from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)
  from PySide2.QtWidgets import *
  
  import twoPin
  
  class Ui_MainWindow(object):
      def setupUi(self, MainWindow):
          self.scene = QtWidgets.QGraphicsScene()
          self.scene.setSceneRect(QtCore.QRectF(0.0, 0.0, 5000.0, 5000.0))
          MainWindow.setObjectName(&quot;MainWindow&quot;)
          MainWindow.resize(800, 600)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName(&quot;centralwidget&quot;)
          self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
          self.verticalLayout.setContentsMargins(0, 0, 0, 0)
          self.verticalLayout.setSpacing(0)
          self.verticalLayout.setObjectName(&quot;verticalLayout&quot;)
          self.graphicsView = QtWidgets.QGraphicsView(self.scene, self.centralwidget)
          self.graphicsView.setObjectName(&quot;graphicsView&quot;)
          self.verticalLayout.addWidget(self.graphicsView)
          MainWindow.setCentralWidget(self.centralwidget)
  
          redBrush = QtGui.QBrush(QtCore.Qt.red)
          blackPen = QtGui.QPen(QtCore.Qt.black)
          blackPen.setWidth(4)
  
          ellipse = self.scene.addEllipse(2300, 2300, 200, 200, blackPen, redBrush)
          ellipse.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
          ellipse.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
          ellipse.setFlag(QtWidgets.QGraphicsItem.ItemIsFocusable)
  
          self.form = QtWidgets.QWidget(self.graphicsView)
          self.widget = twoPin.Ui_Form(self.form)
          self.form.setGeometry(QtCore.QRect(0, 0, 150, 95))
          self.scene.addItem(self.widget)
          self.widget.moveBy(2500, 2500)
  
          self.retranslateUi(MainWindow)
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
  
      def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate(&quot;MainWindow&quot;, &quot;MainWindow&quot;))
  
  
  if __name__ == &quot;__main__&quot;:
      import sys
      app = QtWidgets.QApplication(sys.argv)
      MainWindow = QtWidgets.QMainWindow()
      ui = Ui_MainWindow()
      ui.setupUi(MainWindow)
      MainWindow.show()
      sys.exit(app.exec_())
  </code></pre>