title: PyQt5: Draw a line by clicking QPushButton
body: <p>I am trying to make it such that when I click a QPushButton a line is drawn. However the code I have right now makes the line at the <strong>beginning</strong> when the code is initiated and not <strong>after</strong>. The QPushButton doesn't seem to do any drawing.</p>
  
  <p>I also don't quite understand why when drawing you need an 'event' argument in the function.</p>
  
  <p><a href="https://i.stack.imgur.com/P492b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P492b.png" alt="enter image description here"></a></p>
  
  <pre><code>import sys
  from PyQt5 import QtCore, QtWidgets
  from PyQt5.QtWidgets import QMainWindow, QGridLayout,QPushButton, QApplication, QWidget
  from PyQt5.QtCore import QSize, QCoreApplication, Qt
  from PyQt5.QtGui import QPainter, QPen
  
  class MainWindow(QMainWindow):
      def __init__(self):
          QMainWindow.__init__(self)
  
          self.setMinimumSize(QSize(300, 300)) 
  
          pybutton = QPushButton('button', self)
          pybutton.clicked.connect(self.paintEvent)
          pybutton.resize(100,100)
          pybutton.move(100, 100) 
  
      def paintEvent(self,event):
          print('click')
          painter = QPainter(self)
          pen = QPen(Qt.red, 3)
          painter.setPen(pen)
          painter.drawLine(0,0,100,100)
  
  if __name__ == "__main__":
      app = QtWidgets.QApplication(sys.argv)
      mainWin = MainWindow()
      mainWin.show()
      sys.exit( app.exec_() )
  </code></pre>