Drag and drop technique

There are various ways in which you can transfer data between two objects or applications. Drag and drop is a modern visual technique of transference 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. This is to ensure that the information can be transferred safely between applications. The various MIME types that are supported by Qt include plain text, HTML text, URI-list text, image data, and color data. We will explore the Qt classes that are used for this action and shortly test them 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

This provides an event that is sent to a widget when a drag and drop action enters it

QDragLeaveEvent

This provides an event that is sent to a widget when a drag and drop action leaves it

QDragMoveEvent

This provides an event that is sent to a widget when a drag and drop action is in progress

QDropEvent

This provides an event that is sent to a widget when a drag and drop action is completed

QMimeData

This 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 reimplementing the dragMoveEvent() and dropEvent() functions. As the user drags over the widget, dragMoveEvent() occurs, and dropEvent() occurs 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 the following code:

cclass MyWidget(QWidget):
    def __init__(self):
        super(MyWidget,self).__init__()
        self.initGUI()

    def initGUI(self):
        self.myListWidget1 = MyListWidget(self,None)
        self.myListWidget2 = MyListWidget(self,'ICON')

        self.setGeometry(300, 350, 500, 150)

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

        self.myListWidget1.additem('blue_bird.png',"Angry Bird Blue")
        self.myListWidget1.additem('red_bird.png',"Angry Bird Red")
        self.myListWidget1.additem('green_bird.png',"Angry Bird Green")
        self.myListWidget1.additem('black_bird.png',"Angry Bird Black")
        self.myListWidget1.additem('white_bird.png',"Angry Bird White")

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

        self.setLayout(self.myLayout)
        self.show()

class MyListWidget(QListWidget):

    def __init__(self,parent=None,viewMode=None):
        super(MyListWidget,self).__init__(parent)
        self.initWidget(viewMode)

    def initWidget(self,viewMode=None):
        self.setAcceptDrops(True)
        self.setDragEnabled(True)
        if viewMode == 'ICON':
            self.setViewMode(QListWidget.IconMode)

    def additem(self,fileName, desc):
        QListWidgetItem(QIcon(fileName), desc, self)

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

Drag and drop technique

Both partitions of the application have 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 the dragging mode as they are set with setDragEnabled(True). They also accept the 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 reimplementing the previously discussed 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
3.131.38.14