Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import sys
  2. from PyQt5 import QtWidgets, uic
  3. Ui_MainWindow, QtBaseClass = uic.loadUiType("Op4.ui")
  4.  
  5.  
  6. class CPoint:
  7. def __init__(self, x ,y):
  8. self._x = x
  9. self._y = y
  10.  
  11. class CLine:
  12. def __init__(self, P1, P2):
  13. self._P1 = P1
  14. self._P2 = P2
  15.  
  16. class MyApp(QtWidgets.QMainWindow):
  17. def __init__(self):
  18. self._CurP1 = CPoint(0,0)
  19. self._CurP2 = CPoint(0,0)
  20. self._allPoints = []
  21. super(MyApp, self).__init__()
  22.  
  23. self.ui = Ui_MainWindow()
  24. self.ui.setupUi(self)
  25. self._scene = QtWidgets.QGraphicsScene()
  26. self._scene.setSceneRect(0,0,100,100)
  27. self.ui.graphicsView.setScene(self._scene)
  28. self.ui.graphicsView.mousePressEvent = self.mouseDown
  29. self.ui.graphicsView.mouseReleaseEvent = self.mouseUp
  30. self.ui.graphicsView.mouseMoveEvent = self.mouseMove
  31.  
  32.  
  33. self._allPoints.append(CLine(CPoint(10,10), CPoint(30,50)))
  34. self._allPoints.append(CLine(CPoint(30,50), CPoint(40,20)))
  35. self._allPoints.append(CLine(CPoint(40,20), CPoint(100,35)))
  36. self._allPoints.append(CLine(CPoint(100,35), CPoint(105,60)))
  37. self.drawAllLines()
  38.  
  39. def mouseDown(self, event):
  40. point = self.ui.graphicsView.mapToScene(event.pos())
  41. MX = point.x()
  42. MY = point.y()
  43. self._CurP1 = CPoint(MX,MY)
  44.  
  45.  
  46. def mouseUp(self, event):
  47. point = self.ui.graphicsView.mapToScene(event.pos())
  48. MX = point.x()
  49. MY = point.y()
  50. self._CurP2 = CPoint(MX,MY)
  51. self._allPoints.append(CLine(self._CurP1, self._CurP2))
  52. self.drawAllLines()
  53.  
  54. def mouseMove(self, event):
  55. point = self.ui.graphicsView.mapToScene(event.pos())
  56. MX = point.x()
  57. MY = point.y()
  58. self.drawAllLines()
  59. item = QtWidgets.QGraphicsLineItem(self._CurP1._x, self._CurP1._y, MX,MY)
  60. self._scene.addItem(item)
  61.  
  62.  
  63.  
  64. def drawAllLines(self):
  65. self._scene.clear()
  66. for a in self._allPoints:
  67. item = QtWidgets.QGraphicsLineItem(a._P1._x, a._P1._y, a._P2._x, a._P2._y)
  68. self._scene.addItem(item)
  69.  
  70.  
  71.  
  72. if __name__ == "__main__":
  73. app = 0 # lost Kernal died probeem op bij herhaald opstarten
  74. app = QtWidgets.QApplication(sys.argv)
  75. window = MyApp()
  76. window.show()
  77. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement