0

I want to connect lines between ellipses, and when I move the ellipse the line stays attached when the ellipse moves. So I need to get the position of the ellipses when they move.

When mouse is clicked, an ellipse is added and I save its location in a dictionary.

class Draw(QMainWindow):
    baseDict={}
    i=0
    
    def __init__(self):
        
        super().__init__()
        
        loadUi("drawnetwork.ui",self)
        self.scene=QGraphicsScene()
        self.scene.setSceneRect(0,0,652,379)
        blueBrush=QBrush(Qt.blue)
        self.create_scene(20,21,10,blueBrush)
        greenBrush=QBrush(Qt.green)
        self.create_scene(20,91,10,greenBrush)
        self.connectline.clicked.connect(self.connectLine)
        
        # self.drawbase.clicked.connect(self.toDrawBase)
       
        
        # self.setSceneRect(0,0,1200,1000)
        # print(self.gview.QMouseEvent.pos())
        #self.draw.clicked.connect(self.create_scene)
        #self.create_scene()
          
    def mousePressEvent(self, event):
       
        
        if(self.drawbase.isChecked()):
     
         self.create_scene(event.pos().x(),event.pos().y(),20,QBrush(Qt.green))
         self.baseDict[self.i]=[event.pos().x(),event.pos().y()]
         self.i=self.i+1
        if(self.drawmolecular.isChecked()):
     
         self.create_scene(event.pos().x(),event.pos().y(),20,QBrush(Qt.blue)) 

    
    def mouseReleaseEvent(self, event):
       
      
       print("release")
    #to connect ellipse
    def connectLine(self):
        pen2 = QPen(QBrush(QColor(100,100,100,255)), 3)
        list1=self.baseDict[0]   
        list2=self.baseDict[1] 
        x1=list1[0]
        y1=list1[1]
        x2=list2[0]
        y2=list2[1]

        self.scene.addLine(x1+17,y1+17,x2+2,y2+4,pen2)

   
         
    

    def create_scene(self,x,y,r,brush):
          print("in method") 
          self.gview.setScene(self.scene)
          greenBrush=QBrush(Qt.green)
          blueBrush=QBrush(Qt.blue)
          blackPen=QPen(Qt.black)
          blackPen.setWidth(2)
          ellipse=self.scene.addEllipse(x,y,r,r,blackPen,brush)
          ellipse.setFlag(QGraphicsItem.ItemIsMovable)
          ellipse.setAcceptHoverEvents(True)
         
        #   self.gview.view=QGraphicsView(self.scene,self)
        #   self.gview.view.setGeometry(175,35,386,302)  
          self.show() 
musicamante
  • 41,230
  • 6
  • 33
  • 58
mdl
  • 23
  • 7
  • Not using python but in C++, every QGraphicsItem have a `itemChanged(...)` virtual function you can override to get updates about position and other stuff. Relevant doc: https://doc.qt.io/qt-5/qgraphicsitem.html#itemChange There might be something similar in pyqt – Nico J. May 17 '22 at 10:31
  • Note that your code has many issues: 1. you should not set a dictionary as class attribute (unless you *really* know what you're doing); 2. there are lots of code styling issues, like inconsistent indentation and missing spaces (see the official [Style Guide for Python Code](//python.org/dev/peps/pep-0008)) ; 3. mouse event handlers should be defined in the QGraphicsScene or QGraphicsView subclass, or with an event filter, especially if you want to allow moving items with mouse; 4. calling `setScene()` every time is pointless; 5. the same for `show()`, which doesn't make a lot of sense there. – musicamante May 17 '22 at 12:05

0 Answers0