Drag-and-drop

There are various ways in which you can transfer data between two objects or applications. Drag-and-drop is a modern visual technique of transformation of data between objects. It enables the user to copy and paste very intuitively. The drag-and-drop is a combination of two events, namely "Dragging and Dropping". The widgets can serve as drag sites, drop sites, or as both. One of the important factors that we should take care of is the MIME type of the object that we would drag-or-drop. It is to ensure that the information can be transferred safely between applications. The various MIME types supported by Qt include plain text, html text, uri-list text, image data, and color data. We will explore the Qt classes used for this action and shortly test with an example.

The various classes that are involved in drag-and-drop and their necessary MIME encoding and decoding are listed in the following table:

Class

Description

QDragEnterEvent

Provides an event which is sent to a widget when drag and drop action enters it

QDragLeaveEvent

Provides an event which is sent to a widget when drag and drop action leaves it

QDragMoveEvent

Provides an event which is sent to a widget when drag and drop action is in progress

QDropEvent

Provides an event which is sent to a widget when drag and drop action is completed

QMimeData

Provides a container for data that records information about its MIME type

A drag can be initiated by setting the widget's setDragEnabled() with a Boolean True value. The dropping functionality can be implemented by re-implementing the dragMoveEvent() and dropEvent(). As the user drags over the widget, dragMoveEvent() occur and dropEvent() when the drag event is completed. We will now see an example for the drag-and-drop events and the working of the code will be explained in following the code:

class MyWidget(QWidget):
  def __init__(self):
    QWidget.__init__(self)
    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, 150)

    self.myLayout = QHBoxLayout()
    self.myLayout.addWidget(self.myListWidget1)
    self.myLayout.addWidget(self.myListWidget2)

    l1 = QListWidgetItem(QIcon('blue_bird.png'),"Angry Bird Blue")
    l2 = QListWidgetItem(QIcon('red_bird.png'),"Angry Bird Red")
    l3 = QListWidgetItem(QIcon('green_bird.png'),"Angry Bird Green")
    l4 = QListWidgetItem(QIcon('black_bird.png'),"Angry Bird Black")
    l5 = QListWidgetItem(QIcon('white_bird.png'),"Angry Bird White")

    self.myListWidget1.insertItem(1, l1)
    self.myListWidget1.insertItem(2, l2)
    self.myListWidget1.insertItem(3, l3)
    self.myListWidget1.insertItem(4, l4)
    self.myListWidget1.insertItem(5, l5)

    QListWidgetItem(QIcon('gray_pig.png'), "Grey Pig", self.myListWidget2)
    QListWidgetItem(QIcon('brown_pig.png'), "Brown Pig", self.myListWidget2)
    QListWidgetItem(QIcon('green_pig.png'), "Green Pig", self.myListWidget2)
   
    self.setWindowTitle('Drag and Drop Example'),

    self.setLayout(self.myLayout)

The preceding program on execution will look like the following screenshot:

Drag-and-drop

Both partition of the application has a QListWidget object with some items added to it. The left side is the default view mode of QListWidget and the right side is set to icon view mode. Both these widgets support dragging mode as they are set with setDragEnabled(True). They also accept dropping functionality, as the setAcceptDrops(True) is set. You can test this by dragging-and-dropping between the widgets. We can control the behavior of the application by re-implementing the aforesaid event handler functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset