title: How to draw a rectangle and adjust its shape by drag and drop in PyQt5
body: <p>I'm trying to draw a rectangle on GUI created by PyQt5 by drag and drop. I managed to do that, but the rectangle is drawn when the mouse left key is released.</p>
  
  <p>What I want to do is like this <a href="https://www.youtube.com/watch?v=61pkLt9rWPE" rel="noreferrer">link</a>: </p>
  
  <ul>
  <li>When the mouse left button is pressed, start drawing the rectangle.</li>
  <li>While dragging, adjust the rectangle shape with the mouse movement.</li>
  <li>When the mouse left button is released, determine the rectangle shape.</li>
  </ul>
  
  <p>How can I implement this? Thanks in advance.</p>
  
  <p>Here's my code.</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  import sys
  from PyQt5 import QtWidgets, QtCore
  from PyQt5.QtGui import QPainter
  
  class MyWidget(QtWidgets.QWidget):
      def __init__(self):
          super().__init__()
          self.setGeometry(30,30,600,400)
          self.pos1 = [0,0]
          self.pos2 = [0,0]
          self.show()
  
      def paintEvent(self, event):
          width = self.pos2[0]-self.pos1[0]
          height = self.pos2[1] - self.pos1[1]     
  
          qp = QPainter()
          qp.begin(self)           
          qp.drawRect(self.pos1[0], self.pos1[1], width, height)        
          qp.end()
  
      def mousePressEvent(self, event):
          self.pos1[0], self.pos1[1] = event.pos().x(), event.pos().y()
          print("clicked")
  
      def mouseReleaseEvent(self, event):
          self.pos2[0], self.pos2[1] = event.pos().x(), event.pos().y()
          print("released")
          self.update()
  
  if __name__ == '__main__':
      app = QtWidgets.QApplication(sys.argv)
      window = MyWidget()
      window.show()
      app.aboutToQuit.connect(app.deleteLater)
      sys.exit(app.exec_())
  </code></pre>