Please help,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,but nothing happen
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()
-
Hi,
Pretty simply: add debug statements for example to see if your methods are called. You can also trigger the debugger if you want to see in details what is going on.
Next you did not implement the dragMove event.@SGaist thank you so much ,now it works, all that was missing was the QDragMoveEvent.
-
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,but nothing happen
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()
@electric-dev
Why did you delete your identical question at https://forum.qt.io/topic/163406/drag-and-drop-to-a-qgraphicsview, which I replied to and asked youAnd what happens? Are you going to tell us whether either your
dragEnterEvent()
ordropEvent()
are ever called? -
@electric-dev
Why did you delete your identical question at https://forum.qt.io/topic/163406/drag-and-drop-to-a-qgraphicsview, which I replied to and asked youAnd what happens? Are you going to tell us whether either your
dragEnterEvent()
ordropEvent()
are ever called?@JonB Hello, good evening. I deleted the previous post because I couldn't update the information as you had recommended. A message appeared on the page saying that I had to wait 3600 seconds to make changes to the post, so I waited, but the same message kept appearing.
Regarding this, “And what happens? Are you going to tell us whether either your dragEnterEvent() or dropEvent() are ever called?”, I believe that the dragEnterEvent() and dropEvent() events are implemented in the My_View class, but if this is not the case and it is not too much trouble, please let me know how to do it.
Translated with DeepL.com (free version)
-
Hi,
Pretty simply: add debug statements for example to see if your methods are called. You can also trigger the debugger if you want to see in details what is going on.
Next you did not implement the dragMove event. -
Hi,
Pretty simply: add debug statements for example to see if your methods are called. You can also trigger the debugger if you want to see in details what is going on.
Next you did not implement the dragMove event.@SGaist thank you so much ,now it works, all that was missing was the QDragMoveEvent.
-