Drag and drop to a QgraphicsView

Hello, good morning everyone. I am trying to implement drag and drop from a widget(QPushbutton) located in a QFrame to a view (QgraphicsView), but I am unable to do so. The idea is that when I drag the widget to the view and drop it there, an item (QgraphicsItem) that I call My_Node appears.

Below is part of the code.I hope you can help me.

# this is the widget in the QFrame

class Button(QPushButton):
    def __init__(self):
        super().__init__()
        
        self.setText("2025")

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:

            drag = QDrag(self)
            mime = QMimeData()
            drag.setMimeData(mime)
            drag.exec(Qt.DropAction.MoveAction)

# This is part of the view code

class My_view(QGraphicsView):
    def __init__(self,parent):
     super().__init__(parent)

     
     self.setAcceptDrops(True)
     self.my_scene=My_scene()
     self.setScene(self.my_scene)
   

    def dragEnterEvent(self, event):

      event.acceptProposedAction()
      
      
    def dropEvent(self,event):

      self.mi_nodo=My_Node()

      self.my_scene.addItem(self.mi_nodo) 

      event.acceptProposedAction()


Hello, all that was left was to implement the QDragMoveEvent.Now it works

1 Like