I try hard to understand how to identify an instance of a class within drag and drop, but i need some help.
Beneath I add a sample from "http://zetcode.com/gui/pyqt5/dragdrop/" to explain.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
In this program, we can press on a button with a left mouse
click or drag and drop the button with the right mouse click.
Author: Jan Bodnar
Website: zetcode.com
Last edited: August 2017
"""
from PyQt5.QtWidgets import QPushButton, QWidget, QApplication
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
import sys
class Button(QPushButton):
def __init__(self, title, parent):
super().__init__(title, parent)
def mouseMoveEvent(self, e):
if e.buttons() != Qt.RightButton:
return
mimeData = QMimeData()
drag = QDrag(self)
drag.setMimeData(mimeData)
drag.setHotSpot(e.pos() - self.rect().topLeft())
dropAction = drag.exec_(Qt.MoveAction)
def mousePressEvent(self, e):
super().mousePressEvent(e)
if e.button() == Qt.LeftButton:
print('press')
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setAcceptDrops(True)
self.button = Button('Button', self)
self.button.move(100, 65)
self.setWindowTitle('Click or Move')
self.setGeometry(300, 300, 280, 150)
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
position = e.pos()
self.button.move(position)
e.setDropAction(Qt.MoveAction)
e.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
app.exec_()
Here my questions:
if I instantiate a 2nd button
self.button2 = Button('Button2', self)
self.button2.move(100, 165)
i can also move the button2, but in the DropEvent the first button "button" is moved.
How is it possible to identify within DropEvent, that I move "button2" und place button2?
So: question is: how to identify, which button it is. (i found only drag n drop examples, how to replace a label or a button by another element of same type)
And additional question: to create a new instance works with this type of code. If I create the buttons within QT5 designer, it is not possible to have them as an instance of this type of class Button in this code. So, defining drag n drop in the QT5 designer, drag n drop of an item within a list to another list works, but i am not able to identify, which element I have dragged