I am having trouble maintaining previously drawn lines after I draw a new line. Right now if I click one button it will draw a line but once I click the second button a new line is drawn and the initial one is removed. I would like that both remain.
import sys
from PyQt5.QtWidgets import QMainWindow,QPushButton, QApplication
from PyQt5.QtCore import QSize, Qt, QLine, QPoint
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.draw_line)
pybutton.resize(100,100)
pybutton.move(0, 0)
pybutton2 = QPushButton('button2', self)
pybutton2.clicked.connect(self.draw_line)
pybutton2.resize(100,100)
pybutton2.move(200, 0)
self.line = QLine()
def draw_line(self):
button = self.sender()
x = int(button.x()) + int(button.width())/2
y = int(button.y())+100
self.line = QLine(x, y, x, y+100)
self.update()
def paintEvent(self,event):
QMainWindow.paintEvent(self, event)
if not self.line.isNull():
painter = QPainter(self)
pen = QPen(Qt.red, 3)
painter.setPen(pen)
painter.drawLine(self.line)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())