In this PyQt5 article i want to show you How To Make Drag And Drop Application, we are
going to create a practical example of Drag and Drop Application in PyQt5.
Also you can check more Python articles in the below links
1: Kivy GUI Development Tutorials
2: Python TKinter GUI Development
5: PyQt5 GUI Development Course
Drag And Drop
So drag and drop provides a simple visual mechanism which users can use to transfer information between and within applications. Drag and drop is similar in function to the clipboard’s cut and paste mechanism. This document describes the basic drag and drop mechanism and outlines the approach used to enable it in custom controls. Drag and drop operations are also supported by many of Qt’s controls, such as the item views and graphics view framework, as well as editing controls for Qt Widgets and Qt Quick. More information about item views and graphics view is available in Using drag and drop with item views and Graphics View Framework.
Drag and Drop Classes
QDrag | Support for MIME-based drag and drop data transfer |
QDragEnterEvent | Event which is sent to a widget when a drag and drop action enters it |
QDragLeaveEvent | Event that is sent to a widget when a drag and drop action leaves it |
QDragMoveEvent | Event which is sent while a drag and drop action is in progress |
QDropEvent | Event which is sent when a drag and drop action is completed |
OK now this is the complete code for PyQt5 How To Make Drag And Drop Application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
from PyQt5.QtWidgets import QApplication, QWidget, QListWidget, QHBoxLayout,QListWidgetItem from PyQt5.QtGui import QIcon import sys class Window(QWidget): def __init__(self): super().__init__() self.myListWidget1 = QListWidget() self.myListWidget2 = QListWidget() self.myListWidget2.setViewMode(QListWidget.IconMode) self.myListWidget1.setAcceptDrops(True) self.myListWidget1.setDragEnabled(True) self.myListWidget2.setAcceptDrops(True) self.myListWidget2.setDragEnabled(True) self.setGeometry(300, 350, 500, 300) self.myLayout = QHBoxLayout() self.myLayout.addWidget(self.myListWidget1) self.myLayout.addWidget(self.myListWidget2) l1 = QListWidgetItem(QIcon('cpp.png'), "C++") l2 = QListWidgetItem(QIcon('csharp.png'), "C# ") l3 = QListWidgetItem(QIcon('java.png'), "Java") l4 = QListWidgetItem(QIcon('pythonicon.png'), "Python") self.myListWidget1.insertItem(1, l1) self.myListWidget1.insertItem(2, l2) self.myListWidget1.insertItem(3, l3) self.myListWidget1.insertItem(4, l4) QListWidgetItem(QIcon('html.png'), "HTLM", self. myListWidget2) QListWidgetItem(QIcon('css.png'), "CSS", self. myListWidget2) QListWidgetItem(QIcon('javascript.png'), "Javascript", self. myListWidget2) self.setWindowTitle('Drag and Drop Example'); self.setLayout(self.myLayout) self.show() App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
So in the above code these are our two QListWidgets , and also we have enabled the drag
and drop facility for our two QListWidgets.
1 2 3 4 5 6 7 |
self.myListWidget1 = QListWidget() self.myListWidget2 = QListWidget() self.myListWidget2.setViewMode(QListWidget.IconMode) self.myListWidget1.setAcceptDrops(True) self.myListWidget1.setDragEnabled(True) self.myListWidget2.setAcceptDrops(True) self.myListWidget2.setDragEnabled(True) |
In here we add items to our two QListWidgets, also make sure that you have some
icons in your working directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
l1 = QListWidgetItem(QIcon('cpp.png'), "C++") l2 = QListWidgetItem(QIcon('csharp.png'), "C# ") l3 = QListWidgetItem(QIcon('java.png'), "Java") l4 = QListWidgetItem(QIcon('pythonicon.png'), "Python") self.myListWidget1.insertItem(1, l1) self.myListWidget1.insertItem(2, l2) self.myListWidget1.insertItem(3, l3) self.myListWidget1.insertItem(4, l4) QListWidgetItem(QIcon('html.png'), "HTLM", self. myListWidget2) QListWidgetItem(QIcon('css.png'), "CSS", self. myListWidget2) QListWidgetItem(QIcon('javascript.png'), "Javascript", self. myListWidget2) |
Also every PyQt5 application must create an application object. The sys.argv
parameter is a list of arguments from a command line.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. also we have created the object of our Window class.
1 2 |
window = Window() sys.exit(App.exec()) |
So run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email
Easily done with your chosen widgets as they support drag drop event handling natively. This example does not illustrate or demonstrate how one would need to implement the actual properties, actions and events needed for drag drop feature between commonly used and customized objects that typically do not have the complete implementation done out of the box like the widgets in your tutorial. Maybe you could make a tutorial on drag drop that actually demonstrates how it works by coding a drag drop implementation between simple, commonly used widgets?