PyQt5 Tutorial – Build Drag & Drop Application

In this PyQt5 Tutorial we are going to learn how to Build Drag & Drop Application in PyQt5,

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

 

 

 

Drag and Drop Classes

There are different classes that you can use for Drag & Drop in PyQt5.

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

 

 

 

 

 

After importing the required classes from PyQt5, first we need to create two QListWidgets,

because we want to perform our drag and drop functionalities in the QListWidgets.

 

 

Also we are going to set the QListWidget mode to IconMode.

 

 

 

In here we need to enable PyQt5 Drag and Drop functionalities for the QListWidget.

 

 

We need to create an HBoxLayout, and we are going to add our QListWidget in QHBoxLayout.

 

 

We are going to add some items in our first ListWidget.

 

 

 

Also we want to do the same for our second ListWidget.

 

 

 

 

Run the complete code and this is the result.

PyQt5 Tutorial - Build Drag & Drop Application
PyQt5 Tutorial – Build Drag & Drop Application

Leave a Comment