title: PYQT Draw selection rectangle over picture
body: <p>I'm confused on how to click and draw a rectangle over a picture that the user has loaded in. I found a few examples that I tried to follow but nothing seems to work and I am not sure why or how to go about fixing it.</p>
  
  <p>I have put some breakpoints in and it doesn't seem to be going into the <code>mouseMoveEvent</code> function, but I am not sure why. Any help would be very much appreciated.</p>
  
  <p><strong>What I would like</strong></p>
  
  <p>I would like to be able to click and drag on a picture that I have loaded into <code>QGraphics</code>and have it draw a box and then in the status bar output the X and Y coordinates of both points of the rectangle. I would also like the rectangle to stay there until the user clicks on the picture a second time.</p>
  
  <p><strong>Examples I have found</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/32018223/pyqt4-qgraphicsitem-position-doesnt-map-into-scene-properly-after-drag">What I would like</a></p>
  
  <p><a href="https://stackoverflow.com/questions/44468775/how-to-draw-a-rectangle-and-adjust-its-shape-by-drag-and-drop-in-pyqt5">Simple example</a></p>
  
  <p><strong>Current Code</strong></p>
  
  <pre><code>class MainWindow(QMainWindow, Ui_MainWindow):
      def __init__(self, *args, **kwargs):
          QMainWindow.__init__(self, *args, **kwargs)
          self.setupUi(self)
          self.setUpMainUiFunction()
  
      def setUpMainUiFunction(self):
          self.actionOpen.triggered.connect(self.OpenDialog)
          self.Button_LoadPhoto.clicked.connect(self.OpenDialog)
  
          open = QAction(QIcon("icons/open.bmp"), "open", self)
          save = QAction(QIcon("icons/save.bmp"), "save", self)
          NormalCursor = QAction(QIcon("icons/cursor-normal.png"), "NormalCursor", self)
          CrosshairCursor = QAction(QIcon("icons/crosshair.png"), "CrosshairCursor", self)
  
          self.TopToolBar.addAction(open)
          self.TopToolBar.addAction(save)
          self.LeftToolBar.addAction(NormalCursor)
          self.LeftToolBar.addAction(CrosshairCursor)
  
          # self.TopToolBar.actionTriggered[QAction].connect(self.toolbtnpressed)
  
      def OpenDialog(self):
          options = QFileDialog.Options()
          options |= QFileDialog.DontUseNativeDialog
          PicturePath = QStandardPaths.standardLocations(QStandardPaths.PicturesLocation)[0]
          filenames, _ = QFileDialog.getOpenFileNames(self, "Open File", PicturePath, "JPEG File (*.png)", options=options)
          for filename in filenames:
              pixmap = QPixmap(filename)
              self.showPicture(pixmap)
              self.statusbar.showMessage("Successfully Loaded: {}".format(filename))
  
      def showPicture(self, picture):
          sub = QMdiSubWindow(self)
          loadPicture = LoadPicture(picture, sub)
          sub.setWidget(loadPicture)
          sub.setObjectName("Load_Picture_window")
          sub.setWindowTitle("New Photo")
          self.mdiArea.addSubWindow(sub)
          sub.show()
          sub.resize(picture.size())
          loadPicture.log.MousePixmapSignal.connect(self.updatePixel)
  
      def updatePixel(self, point, color):
          self.UserInput_PixelValue_X.setText("{}".format(point.x()))
          self.UserInput_PixelValue_Y.setText("{}".format(point.y()))
  
          self.UserInput_PixelValue_R.setText("{}".format(color.red()))
          self.UserInput_PixelValue_G.setText("{}".format(color.green()))
          self.UserInput_PixelValue_B.setText("{}".format(color.blue()))
  </code></pre>
  
  <p>This is in a seprate file.</p>
  
  <pre><code>class LogObject(QObject):
      MousePixmapSignal = pyqtSignal(QPoint, QColor)
  
  
  class PictureItem(QGraphicsPixmapItem):
      def __init__(self, log, *args, **kwargs):
          QGraphicsPixmapItem.__init__(self, *args, **kwargs)
          self.setAcceptHoverEvents(True)
          self.log = log
  
      def hoverMoveEvent(self, event):
          point = event.pos().toPoint()
          color = QColor(self.pixmap().toImage().pixel(point.x(), point.y()))
          self.log.MousePixmapSignal.emit(point, color)
          QGraphicsPixmapItem.hoverMoveEvent(self, event)
  
      def hoverEnterEvent(self, event):
          QApplication.setOverrideCursor(Qt.CrossCursor)
          QGraphicsPixmapItem.hoverMoveEvent(self, event)
  
      def hoverLeaveEvent(self, event):
          QApplication.setOverrideCursor(Qt.ArrowCursor)
          QGraphicsPixmapItem.hoverLeaveEvent(self, event)
  
  
      def paintEvent(self, event):
          qp = QPainter(self)
          br = QBrush(QColor(100, 10, 10, 40))
          qp.setBrush(br)
          qp.drawRect(QRect(self.begin, self.end))
  
     def mousePressEvent(self, event):
         self.begin = event.pos()
         self.end = event.pos()
         QGraphicsPixmapItem.mousePressEvent(self, event)
         self.update()
  
     def mouseMoveEvent(self, event):
         self.end = event.pos()
         QGraphicsPixmapItem.mouseMoveEvent(self, event)
         self.update()
  
     def mouseReleaseEvent(self, event):
         self.begin = event.pos()
         self.end = event.pos()
         QGraphicsPixmapItem.mouseReleaseEvent(self, event)
         self.update()   
  
  class LoadPicture(QWidget, Ui_GraphicsArea):
      def __init__(self, pixmap, parent=None):
          QWidget.__init__(self, parent)
          self.setupUi(self)
          self.log = LogObject(self)
          self.PictureArea.setScene(QGraphicsScene())
          self.item = PictureItem(self.log, pixmap)
          self.PictureArea.scene().addItem(self.item)
          self.resize(pixmap.size())
  </code></pre>