Last time continued
Drag & drop I will summarize this site roughly in Japanese.
Simple_drag_and_drop.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QPushButton, QWidget, 
    QLineEdit, QApplication)
#Inherit QPushButton
class Button(QPushButton):
  
    def __init__(self, title, parent):
        super().__init__(title, parent)
        
        #Allows drop operation for buttons
        self.setAcceptDrops(True)
        
    def dragEnterEvent(self, e):
        
        #Set draggable data format
        if e.mimeData().hasFormat('text/plain'):
            e.accept()
        else:
            e.ignore() 
    def dropEvent(self, e):
        
        #Swap button labels when dropped
        self.setText(e.mimeData().text()) 
class Example(QWidget):
  
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):
        edit = QLineEdit('', self)
        #Make it draggable
        edit.setDragEnabled(True)
        edit.move(30, 65)
        button = Button("Button", self)
        button.move(250, 65)
        
        self.setWindowTitle('Simple drag & drop')
        self.setGeometry(300, 300, 300, 150)
if __name__ == '__main__':
  
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()
initial state

After drag and drop

Drag&drop_a_button_widget.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QPushButton, QWidget, QApplication
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
class Button(QPushButton):
  
    def __init__(self, title, parent):
        super().__init__(title, parent)
        
    def mouseMoveEvent(self, e):
        #Right-click to allow drag and drop
        if e.buttons() != Qt.RightButton:
            return
        #Substitute the data format to be dragged and dropped
        mimeData = QMimeData()
        drag = QDrag(self)
        drag.setMimeData(mimeData)
        #Set the upper left of the button at the dropped position
        drag.setHotSpot(e.pos() - self.rect().topLeft())
        dropAction = drag.exec_(Qt.MoveAction)
    def mousePressEvent(self, e):
      
        #Button color change when the button is pressed
        QPushButton.mousePressEvent(self, e)
        
        #Press display on console when left-clicked
        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):
        #Place the button at the mouse position after dragging
        position = e.pos()
        self.button.move(position)
        #Well I do not know
        e.setDropAction(Qt.MoveAction)
        e.accept()
        
if __name__ == '__main__':
  
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()  
 
Next time will try Painting roughly.
Recommended Posts