[GUI with Python] PyQt5-Drag and drop-

Last time continued

Drag & drop I will summarize this site roughly in Japanese.

[Drag and drop to button]

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 dradro_syoki.png

After drag and drop dradro_after.png

[Drag and drop the button]

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_()  

drag_drop_middle.png  drag_drop_after.png

Next time will try Painting roughly.

Recommended Posts

[GUI with Python] PyQt5-Drag and drop-
Happy GUI construction with electron and python
Drag and drop local files with Selenium (Python)
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
[GUI with Python] PyQt5-Layout management-
python with pyenv and venv
[GUI with Python] PyQt5 -Preparation-
Works with Python and R
[GUI with Python] PyQt5 -Paint-
Display and shoot webcam video with Python Kivy [GUI]
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
[GUI with Python] PyQt5 -Widget II-
Let's make a GUI with python.
Scraping with Python, Selenium and Chromedriver
Scraping with Python and Beautiful Soup
[GUI with Python] PyQt5-The first step-
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
Reading and writing NetCDF with Python
I played with PyQt5 and Python3
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
I tried to make GUI tic-tac-toe with Python and Tkinter
[GUI with Python] PyQt5 -Custom Widget-
[GUI in Python] PyQt5-Menu and Toolbar-
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python
Communicate between Elixir and Python with gRPC
Calculate and display standard weight with python
Monitor Mojo outages with Python and Skype
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Passwordless authentication with RDS and IAM (Python)
Using Python and MeCab with Azure Databricks
POST variously with Python and receive with Flask
Execute Google Translate and DeepL Translate with GUI
Fractal to make and play with Python
A memo with Python2.7 and Python3 on CentOS
Use PIL and Pillow with Cygwin Python
Create and decrypt Caesar cipher with python
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Reading and writing JSON files with Python
Dealing with "years and months" in Python
I installed and used Numba with Python3.5
Tweet analysis with Python, Mecab and CaboCha
Linking python and JavaScript with jupyter notebook
Traffic monitoring with Kibana, ElasticSearch and Python
FM modulation and demodulation with Python Part 2
Encrypt with Ruby (Rails) and decrypt with Python
Easily download mp3 / mp4 with python and youtube-dl!