Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PyQt5 import QtWidgets, uic
- Ui_MainWindow, QtBaseClass = uic.loadUiType("Op4.ui")
- class CPoint:
- def __init__(self, x ,y):
- self._x = x
- self._y = y
- class CLine:
- def __init__(self, P1, P2):
- self._P1 = P1
- self._P2 = P2
- class MyApp(QtWidgets.QMainWindow):
- def __init__(self):
- self._CurP1 = CPoint(0,0)
- self._CurP2 = CPoint(0,0)
- self._allPoints = []
- super(MyApp, self).__init__()
- self.ui = Ui_MainWindow()
- self.ui.setupUi(self)
- self._scene = QtWidgets.QGraphicsScene()
- self._scene.setSceneRect(0,0,100,100)
- self.ui.graphicsView.setScene(self._scene)
- self.ui.graphicsView.mousePressEvent = self.mouseDown
- self.ui.graphicsView.mouseReleaseEvent = self.mouseUp
- self.ui.graphicsView.mouseMoveEvent = self.mouseMove
- self._allPoints.append(CLine(CPoint(10,10), CPoint(30,50)))
- self._allPoints.append(CLine(CPoint(30,50), CPoint(40,20)))
- self._allPoints.append(CLine(CPoint(40,20), CPoint(100,35)))
- self._allPoints.append(CLine(CPoint(100,35), CPoint(105,60)))
- self.drawAllLines()
- def mouseDown(self, event):
- point = self.ui.graphicsView.mapToScene(event.pos())
- MX = point.x()
- MY = point.y()
- self._CurP1 = CPoint(MX,MY)
- def mouseUp(self, event):
- point = self.ui.graphicsView.mapToScene(event.pos())
- MX = point.x()
- MY = point.y()
- self._CurP2 = CPoint(MX,MY)
- self._allPoints.append(CLine(self._CurP1, self._CurP2))
- self.drawAllLines()
- def mouseMove(self, event):
- point = self.ui.graphicsView.mapToScene(event.pos())
- MX = point.x()
- MY = point.y()
- self.drawAllLines()
- item = QtWidgets.QGraphicsLineItem(self._CurP1._x, self._CurP1._y, MX,MY)
- self._scene.addItem(item)
- def drawAllLines(self):
- self._scene.clear()
- for a in self._allPoints:
- item = QtWidgets.QGraphicsLineItem(a._P1._x, a._P1._y, a._P2._x, a._P2._y)
- self._scene.addItem(item)
- if __name__ == "__main__":
- app = 0 # lost Kernal died probeem op bij herhaald opstarten
- app = QtWidgets.QApplication(sys.argv)
- window = MyApp()
- window.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement